<?php
header('Content-type: text/json;charset=utf-8');
$_POST ? $data = $_POST : $data = $_GET;
$id = $data['id'];

if (!$id && !$data['taobao_user_nick']) {
    exit();
}
require_once "config.php";
require_once "MyPDO.class.php";


$beginToday = mktime(0, 0, 0, date('m'), date('d') - 1, date('Y'));//昨日起始
$endToday   = mktime(0, 0, 0, date('m'), date('d'), date('Y')) - 1;//昨日末尾

$beginToday2 = mktime(0, 0, 0, date('m'), date('d'), date('Y'));//昨日起始
$endToday2   = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;//昨日末尾

$starttime = explode(' ', microtime());
$db        = MyPDO::getInstance($host, $user, $pass, $dbName, 'utf8');
$time      = time();
if ($data['taobao_user_nick']) {
    $update['status'] = 1;
    $taobao           = $data['taobao_user_nick'];
    $cooperation_shop = $db->query("cooperation_shop", "`taobao` = '$taobao' AND `status` = '0'", "Row");
    $db->update("cooperation_shop", $update, "`taobao` = '$taobao' AND `status` = '0'");

    file_get_contents("http://114.55.144.79/?session=" . $data['session'] . "&refresh_token=" . $data['refresh_token'] . "&taobao_user_nick=" . $data['taobao_user_nick'] . "&taobao=" . $cooperation_shop['shop']);
    header("Location: http://sj.8qy2.cn/mdd/bindingshops");
}

$admin = $_COOKIE['user'];
if ($data['test'] == '手机端') {
    $admin = $data['user'];
    //$data['model'] = 'test';
}
if ($data['model'] == 'test') {
    $admin = '测试运营号请勿激活';
}
$adminInfo = $db->query("cooperation", "`user` = '$admin'", "Row");
if ($data['model'] != 'test') {
    if ($_COOKIE['sign'] != md5('jdh9lq' . $adminInfo['password'] . $admin)) {
        exit();
    }
}
switch ($id) {
    case "moneyToLld":
        $lld = $data['lld'];
        if ($lld < $adminInfo['money']) {
            $json['status'] = "error";
            $json['msg']    = "余额不足";
            break;
        }
        $json['status']      = "error";
        $json['msg']         = "转换失败";
        $update['money']     = $adminInfo['money'] - $lld;
        $update['principal'] = $adminInfo['principal'] + $lld;
        if ($db->update("cooperation", $update, "`user` = '$adminUser'")) {
            $json['status'] = "success";
            $json['msg']    = "转换成功";
        }
        break;
    case "deleteSjBlackC":
        $bid            = $data['bid'];
        $json['status'] = "error";
        $json['msg']    = "提交失败";
        if ($status = $db->delete('sj_blackC', "`id` = '$bid'")) {
            $json['status'] = "success";
            $json['msg']    = "提交成功";
        }
        break;
    case "getSjBlackC":
        $search     = json_decode($data['search'], true);
        $searchName = $search['search'];
        $sql        = " `user` = '$admin' ";
        if ($search['searchValue'] != '') {
            $searchValue = $search['searchValue'];
            $sql         .= "AND `$searchName` LIKE '%$searchValue%' ";
        }
        $total = $db->getCount('sj_blackC', '`id`', $sql);
        $pn    = ($search['pn'] - 1) * 10;
        $sql   .= " ORDER BY `id` DESC LIMIT $pn,10";
        $arr   = $db->query("sj_blackC", $sql);

        $json['status'] = "success";
        $json['msg']    = "ok";
        $json['total']  = $total;
        $json['data']   = $arr;
        break;
    case "upSjBlackC":
        $name               = $data['name'];
        $update['name']     = $name;
        $update['add_time'] = date("Y-m-d H:i:s");
        $update['admin']    = $admin;
        $update['note']     = $data['note'];
        $json['status']     = "error";
        $json['msg']        = "提交失败";
        if ($db->insert("sj_blackC", $update)) {
            $json['status'] = "success";
            $json['msg']    = "提交成功";
        }
        break;
    case "getSjHelp":
        $search     = json_decode($data['search'], true);
        $searchName = $search['search'];
        $sql        = " `user` = '$admin' ";
        if ($search['searchValue'] != '') {
            $searchValue = $search['searchValue'];
            $sql         .= "AND `$searchName` LIKE '%$searchValue%' ";
        }
        $total = $db->getCount('sj_help', '`id`', $sql);
        $pn    = ($search['pn'] - 1) * 10;
        $sql   .= " ORDER BY `id` DESC LIMIT $pn,10";
        $arr   = $db->query("sj_help", $sql);

        $json['status'] = "success";
        $json['msg']    = "ok";
        $json['total']  = $total;
        $json['data']   = $arr;
        break;
    case "upSjHelp":
        $update['text']     = $data['text'];
        $update['img']      = $data['img'];
        $update['admin']    = $admin;
        $update['add_time'] = date("Y-m-d H:i:s");
        $update['note']     = $data['note'];
        $json['status']     = "error";
        $json['msg']        = "提交失败";
        if ($db->insert("sj_help", $update)) {
            $json['status'] = "success";
            $json['msg']    = "提交成功";
        }
        break;
    case "deleteSjBlack":
        $bid            = $data['bid'];
        $json['status'] = "error";
        $json['msg']    = "提交失败";
        if ($status = $db->delete('sj_black', "`id` = '$bid'")) {
            $json['status'] = "success";
            $json['msg']    = "提交成功";
        }
        break;
    case "getSjBlack":
        $search     = json_decode($data['search'], true);
        $searchName = $search['search'];
        $sql        = " `user` = '$admin' ";
        if ($search['searchValue'] != '') {
            $searchValue = $search['searchValue'];
            $sql         .= "AND `$searchName` LIKE '%$searchValue%' ";
        }
        $total = $db->getCount('sj_black', '`id`', $sql);
        $pn    = ($search['pn'] - 1) * 10;
        $sql   .= " ORDER BY `id` DESC LIMIT $pn,10";
        $arr   = $db->query("sj_black", $sql);

        $json['status'] = "success";
        $json['msg']    = "ok";
        $json['total']  = $total;
        $json['data']   = $arr;
        break;
    case "upSjBlack":
        $order              = $data['order'];
        $taskInfo           = $db->query("tasklist", "`order` = '$order'", "Row");
        $update['user']     = $taskInfo['user'];
        $update['admin']    = $admin;
        $update['up_time']  = date("Y-m-d H:i:s",$dta['up_time'] * 86400 * 30 + time()) ;
        $update['add_time'] = date("Y-m-d H:i:s");
        $update['note']     = $data['note'];
        $json['status']     = "error";
        $json['msg']        = "提交失败";
        if ($db->insert("sj_black", $update)) {
            $json['status'] = "success";
            $json['msg']    = "提交成功";
        }
        break;
    case "getGroupNum":
        $time                   = time();
        $day10Time              = time() - 10 * 24 * 60 * 60;
        $day7Time               = time() - 7 * 24 * 60 * 60;
        $day2Time               = time() - 30 * 60 * 60;
        $f10                    = time() - 10 * 60;
        $day1Time               = time() - 22 * 60 * 60;
        $json['f']['userOkNum'] = $db->getNum("users", "1", " `taskEndTime` <= '$day1Time'  AND `endLoginTime` >= '$day7Time' AND `userGroup` = 'f'") * 1.5;
        $json['z']['userOkNum'] = $db->getNum("users", "1", " `taskEndTime` <= '$day1Time'  AND `endLoginTime` >= '$day7Time' AND `taobao2` != ''") * 1.5;
        break;
    case "getTK":
        $search     = json_decode($data['search'], true);
        $searchName = $search['search'];
        $sql        = " `user` = '$admin' ";
        if ($search['searchValue'] != '') {
            $searchValue = $search['searchValue'];
            $sql         .= "AND `$searchName` LIKE '%$searchValue%' ";
        }

        $total = $db->getCount('tklist', '`id`', $sql);
        $pn    = ($search['pn'] - 1) * 10;
        $sql   .= " ORDER BY `id` DESC LIMIT $pn,10";
        $arr   = $db->query("tklist", $sql);

        foreach ($arr as $k => $v) {
            $taskInfoId                      = $v['task_id'];
            $taskInfo                        = $db->query("tasklist", "`id` = '$taskInfoId'", "Row");
            $user                            = $taskInfo['user'];
            $userInfo                        = $db->query("users", "`user` = '$user'", "Row");
            $arr[$k]['taobao']               = $userInfo['taobao'];
            $arr[$k]['users']                = $userInfo['user'];
            $arr[$k]['sfz']                  = $userInfo['sfz'];
            $arr[$k]['note']                 = $taskInfo['refund_handle_finally'];
            $arr[$k]['refund_handle_status'] = $taskInfo['refund_handle_status'];
            $arr[$k]['task_time']            = date("Y-m-d H:i:s", $taskInfo['addTime']);
            $arr[$k]['shop']                 = $taskInfo['shop'];
            $arr[$k]['babyName']             = $taskInfo['babyName'];
            $arr[$k]['order']                = $taskInfo['order'];
            $taskInfo['send_time'] == '' ?: $arr[$k]['refund_time'] = $taskInfo['send_time'];

            $cooperation_price     = $db->query("cooperation_price", "`user` = '$admin'", "Row");
            $cooperation_price_arr = json_decode($cooperation_price['info'], 1);
            $arr[$k]['comMoney']   = getComMoney($v['money']);
            $arr[$k]['sjComMoney'] = getSjComMoney($v['money'], $cooperation_price_arr);
        }
        $beginToday     = date("Y-m-d");
        $json['status'] = "success";
        $json['msg']    = "ok";
        $json['total']  = $total;
        $json['data']   = $arr;
        break;
    case "getNotice":
        $nid          = $data['nid'];
        $arr          = $db->query("notice", " `id` = '$nid'", "Row");
        $json['data'] = $arr;
        break;
    case "getNoticeList":
        $arr          = $db->query("notice", " `to_user` = 1 ORDER BY `notice`.`id` DESC");
        $json['data'] = $arr;
        break;
    case "getRefundfoList":


        $search     = json_decode($data['search'], true);
        $searchName = $search['search'];
        $sql        = "  `person` = '$admin' AND `status` in ('1001','1006') ";
        !$data['type'] || $data['type'] == 0 ? '' : $sql .= " AND `withdrawStatus` = 2 AND `withdrawNo` != '' ";

        if ($search['searchValue'] != '') {
            $searchValue = $search['searchValue'];
            $sql         .= "AND `$searchName` = '$searchValue' ";
        }

        if ($search['filter']['status'] !== '' && isset($search['filter']['status'])) {
            $status = $search['filter']['status'];
            $sql    .= " AND `status` = '$status' ";
        }
        if ($search['filter']['payStatus'] !== '' && isset($search['filter']['payStatus'])) {
            $payStatus = $search['filter']['payStatus'];
            $sql       .= " AND `payStatus` = '$payStatus' ";
        }
        if ($search['filter']['buyer_rate'] !== '' && isset($search['filter']['buyer_rate'])) {
            $buyer_rate = $search['filter']['buyer_rate'];
            $sql        .= " AND `buyer_rate` = '$buyer_rate' ";
        }
        $sql .= " AND `refund_status` in ('退款成功','买家已经申请退款，等待卖家同意') ";

        if ($search['filter']['upStatus'] !== '' && isset($search['filter']['upStatus'])) {
            $upStatus = $search['filter']['upStatus'];

            $upStatus == '1' ? $sql .= " AND `text1` != '' AND `status` ='1001' AND `buyer_rate` !='已评价' " : $sql .= " AND `text1` = '' AND `status` ='1001' AND `buyer_rate` !='已评价' ";
        }

        if ($search['filter']['goodcomment'] !== '' && isset($search['filter']['goodcomment'])) {
            $status = $search['filter']['goodcomment'];
            if ($status) $sql .= " AND `goodcomment` = '1' ";
        }
        $data['dispose'] == 1 ? $sql .= " AND `refund_handle_status` = 0" : "";
        $total = $db->getCount('tasklist', '`id`', $sql);
        $pn    = ($search['pn'] - 1) * 20;


        $data['tkTime'] == 1 ? $sql .= " ORDER BY `send_time` DESC LIMIT $pn,20" : $sql .= " ORDER BY `time` DESC LIMIT $pn,20";
        $arr = $db->query("tasklist", $sql, "All");

        foreach ($arr as $k => $v) {
            $userOpenid           = $v['user'];
            $userInfo             = $db->query("users", "`user` = '$userOpenid'", "Row");
            $taobaoId             = $userInfo['bindwwid'];
            $arr[$k]['adminUser'] = $userInfo['adminUser'];
            $arr[$k]['sfz']       = $userInfo['sfz'];
            $userName             = $userInfo['user'];
            $year                 = $userInfo['sfz'][6] . $userInfo['sfz'][7] . $userInfo['sfz'][8] . $userInfo['sfz'][9];
            $age                  = date("Y") - $year;

            $okNum = $db->getCount('tasklist', "id", "`user` = '$userName' AND `status` in ('1001','1006') ");
            // if ($taobaoId != -1)

            switch ($okNum) {
                case $okNum < 1:
                    $arr[$k]['maxMoney'] = 100;
                    break;
                case $okNum < 4:
                    $arr[$k]['maxMoney'] = 200;
                    break;
                case $okNum < 6:
                    $arr[$k]['maxMoney'] = 300;
                    break;
                case $okNum < 12:
                    $arr[$k]['maxMoney'] = 400;
                    break;
            }


            $taobaoInfo = $db->query("tbuserinfo", "`Id` = '$taobaoId'", "Row");

            if ($taobaoInfo['buypoint'] < 60 && $taobaoInfo['id']) {
                $arr[$k]['maxMoney'] = 30;
            }

            if ($userInfo['rz_imgs'] == '') {
                //$arr[$k]['maxMoney'] = 30;
            }


            !$taobaoInfo['Id'] ?: $taobaoInfo['regtime'] = date("y-m-d", strtotime($taobaoInfo['regtime']));

            !$taobaoInfo['sex'] ?: $taobaoInfo['sex'] = preg_replace("/号/", '', $taobaoInfo['sex']);

            $userInfo['addTime'] = (time() - $userInfo['addTime']) / 86400;
            $city                = curlGet("http://www.7s6u.cn/api/ip.php?ip=" . $userInfo['ip']);
            $city                = explode("|", $city);

            $userInfo['receiver_address'] = $city[1] . $city[2];
            $arr[$k]['userInfo']          = $userInfo;
            $arr[$k]['zmoney']            = abs($db->getNum("billing", "`change`", "`change` < 0 AND `user` = '$userName'"));
            $arr[$k]['age']               = $age;
            $arr[$k]['okNum']             = $okNum;
            $teacher                      = $userInfo['teacherUser'];

            //$userInfo                     = $db->query("users", "`user` = '$teacher'", "Row");
            $arr[$k]['teacher']     = $teacher;
            $arr[$k]['teacherUser'] = $teacher;
            $arr[$k]['taobaoInfo']  = $taobaoInfo;


            $sql                   = "`teacherUser` = '$teacher'";
            $arr[$k]['group_num']  = $db->getCount("users", "1", $sql);
            $sql                   = "`teacherUser` = '$userName'";
            $arr[$k]['group_num2'] = $db->getCount("users", "1", $sql);

            if ($arr[$k]['group_num'] < 5) {
                $arr[$k]['maxMoney'] = 30;
            }
            $role = $db->query("users", "`teacherUser` = '$userName'", "Row");


            $role['id'] ? $arr[$k]['role'] = "师傅" : $arr[$k]['role'] = "徒弟";

            //$order_Fequency = mt_rand(80, 112);
            if ($arr[$k]['withdrawStatus'] == 2 AND $arr[$k]['withdrawNo'] != '') {

                $order_Fequency            = mt_rand(22, 26);
                $arr[$k]['order_Fequency'] = $order_Fequency + 72;
                $order_Fequency            = $arr[$k]['order_Fequency'];

                $sql = "UPDATE `users` SET `order_Fequency` =  `order_Fequency` + 48 WHERE `teacherUser` = '$teacher' AND `order_Fequency` < 30  AND `integral` < 10";

                $db->execSql($sql);
                $sql                 = "UPDATE `users` SET `order_Fequency` =  $order_Fequency WHERE `user` = '$userName' AND (`order_Fequency` < 30 OR `order_Fequency` >= 72)";
                $arr[$k]['maxMoney'] = 500 - $arr[$k]['maxMoney'] * 0.3 - 800;
            } else {
                $order_Fequency            = 22;
                $arr[$k]['order_Fequency'] = $order_Fequency;
                $order_Fequency            = 0;
                $sql                       = "UPDATE `users` SET `order_Fequency` =  22 WHERE `teacherUser` = '$teacher' AND `order_Fequency` > 30 ";
                $arr[$k]['maxMoneyGroup']  = 500 - $arr[$k]['maxMoney'] * 0.3 - 800;
            }
            $db->execSql($sql);
            $arr[$k]['taskListCountZgroupNo'] = $db->getCount('users', "1", "(`teacherUser` = '$teacher') AND `userStatus` = 5 ", false);
            $arr[$k]['order_Fequency_group']  = $order_Fequency;
            $arr[$k]['time']                  = date("m-d H:i");

            $taskId      = $v['id'];
            $refund_type = $db->query("task_complain", "`task_id` = '$taskId'", "Row");
            $refund_type['id'] ? $arr[$k]['type'] = $refund_type['type'] : $arr[$k]['type'] = 2;
            $taobao            = $v['taobao'];
            $task_money_status = $db->query("taobai_weight", "`taobao` = '$taobao'", "Row");
            $arr[$k]['weight'] = -1;
            if ($task_money_status['id']) {
                $arr[$k]['weight'] = $task_money_status['weight'];
            }
            $arr[$k]['black']  = -1;
            $task_money_status = $db->query("taobao_qz", "`taobao` = '$taobao'", "Row");
            if ($task_money_status['id']) {
                $arr[$k]['black'] = $task_money_status['jq'];
                $jsonss           = json_decode($task_money_status['json'], 1);
                preg_match_all("/<td>(\d+)/", $jsonss['sitableData'], $values);
                preg_match_all("/<td>(\D+?)</", $jsonss['sitableData'], $names);
                foreach ($values[1] as $kk => $vv) {
                    $blackTips[$kk]['name']  = $names[1][$kk + 1];
                    $blackTips[$kk]['value'] = $vv;
                }
                $arr[$k]['blackTips'] = $blackTips;
            }


            $refund_handle_finally = $v['refund_handle_finally'];
            preg_match_all("/处理人：(.+?)-/", $refund_handle_finally, $refund_handle_finally_Info);
            $arr[$k]['one'] = $refund_handle_finally_Info[1][0];
            $arr[$k]['two'] = $refund_handle_finally_Info[1][1];

            $principal             = $v['principal'];
            $person                = $v['person'];
            $cooperation_price     = $db->query("cooperation_price", "`user` = '$person'", "Row");
            $cooperation_price_arr = json_decode($cooperation_price['info'], 1);
            $arr[$k]['comMoney']   = getComMoney($principal);
            $arr[$k]['sjComMoney'] = getSjComMoney($principal, $cooperation_price_arr);

        }


        $refundfom = date("Y-m");
        $refundfod = date("Y-m-d");


        $json['status'] = "success";
        $json['total']  = $total;
        $json['data']   = $arr;
        $json['msg']    = "ok";

        break;

    case "addCooperateTemplate":

        $update['text'] = $data['data'];
        $update['name'] = $_POST['name'];

        if ($_POST['templateId'] == 0) {
            $update['addTime']        = $time;
            $update['cooperate_user'] = $admin;
            $db->insert("task_template", $update, "`cooperate_user` = '$admin'");
            $json['status'] = "success";
            $json['msg']    = "保存成功";
        } else {
            $templateId = $_POST['templateId'];
            $db->update("task_template", $update, "`cooperate_user` = '$admin' ANd `id`= '$templateId' ");
            $json['status'] = "success";
            $json['msg']    = "修改成功";
        }

        break;
    case "getCooperateTemplate":
        $search       = json_decode($data['data'], true);
        $templete_id  = $search['templete_id'];
        $templateInfo = $db->query("task_template", "`cooperate_user` = '$admin' AND `id`= '$templete_id' ", "Row");
        if (!$templateInfo['id']) {
            $templateInfo = $db->query("task_template", "`id` = 1", "Row");
        }
        $arr          = json_decode($templateInfo['text'], 1);
        $json['data'] = $arr;
        $json['name'] = $templateInfo['name'];
        break;
    case "subcommentDemo":
        if ($_POST['babyId'] == '') {
            $json['status'] = "error";
            $json['msg']    = "宝贝ID不为空";
            break;
        }
        if ($_POST['content'] == '') {
            $json['status'] = "error";
            $json['msg']    = "默认评价不为空";
            break;
        }
        if ($_POST['contentAdd'] == '') {
            $json['status'] = "error";
            $json['msg']    = "追加评价不为空";
            break;
        }
        $adminUser  = $adminInfo['user'];
        $insertData = [
            'babyId'  => $_POST['babyId'],  //宝贝id
            'text1'   => $_POST['content'],  //首评
            'img1'    => json_encode($_POST['imgArr']),  //首评图片
            'text2'   => $_POST['contentAdd'], //追评
            'img2'    => json_encode($_POST['imgArrAdd']),  //追评图片
            'status'  => 0,
            'addTime' => time(),
            'up_user' => $adminUser,
            'qn'      => 1,
        ];
        $status     = $db->insert('backup_new', $insertData);

        $json['status'] = "success";
        $json['msg']    = "提交默认上图好评成功啦";
        break;
    case "subcommentDemo2":
        if ($_POST['babyId'] == '') {
            $json['status'] = "error";
            $json['msg']    = "宝贝ID不为空";
            break;
        }
        if ($_POST['contentAdd'] == '') {
            //$json['status'] = "error";
            //$json['msg']    = "追加评价不为空";
            //break;
        }
        $adminUser  = $adminInfo['user'];
        $insertData = [
            'babyId'  => $_POST['babyId'],  //宝贝id
            'text1'   => $_POST['content'],  //首评
            'img1'    => json_encode($_POST['imgArr']),  //首评图片
            'text2'   => $_POST['contentAdd'], //追评
            'img2'    => json_encode($_POST['imgArrAdd']),  //追评图片
            'status'  => 0,
            'addTime' => time(),
            'up_user' => $adminUser,
            'qn'      => 1,
        ];
        $status     = $db->insert('backup_new_z', $insertData);

        $json['status'] = "success";
        $json['msg']    = "提交追加上图好评成功啦";
        break;
    case "getBackupNew":
        $adminUser = $adminInfo['user'];
        $search    = json_decode($data['search'], true);

        $sql   = " `up_user` = '$adminUser' ";
        $pn    = ($search['pn'] - 1) * 10;
        $total = $db->getCount('backup_new', '`id`', $sql);
        $sql   .= " ORDER BY `id` DESC LIMIT $pn,10";

        $arr = $db->query("backup_new", $sql);
        foreach ($arr as $k => $v) {
            $arr[$k]['img1'] = json_decode($v['img1']);
            $arr[$k]['img2'] = json_decode($v['img2']);
        }


        $json['data']  = $arr;
        $json['total'] = $total;

        $json['status'] = "success";
        $json['msg']    = "查看成功";
        break;
    case "getBackupNewZ":
        $adminUser = $adminInfo['user'];
        $search    = json_decode($data['search'], true);

        $sql = " `up_user` = '$adminUser' ";

        $pn    = ($search['pn'] - 1) * 10;
        $total = $db->getCount('backup_new_z', '`id`', $sql);
        $sql   .= " ORDER BY `id` DESC LIMIT $pn,10";

        $arr = $db->query("backup_new_z", $sql);
        foreach ($arr as $k => $v) {
            //            $arr[$k]['img1'] = json_decode($v['img1']);
            $arr[$k]['img2'] = json_decode($v['img2']);
        }
        $json['data']  = $arr;
        $json['total'] = $total;

        $json['status'] = "success";
        $json['msg']    = "查看成功";
        break;
    case "getGoodCommentList":
        $arr = $db->query("goodcomment", " 1 ORDER BY `id` DESC ");
        foreach ($arr as $k => $v) {
            if ($v['person'] == '') {
                $order            = $v['order'];
                $taskListInfo     = $db->query('tasklist', "`order` = '$order'", "Row");
                $taskId           = $taskListInfo['taskId'];
                $taskInfo         = $db->query('task', "`id` = '$taskId'", "Row");
                $update['person'] = $taskInfo['person'];
                $update['admin']  = $taskInfo['user'];
                $goodcommentId    = $v['id'];
                $db->update('goodcomment', $update, "`id` = '$goodcommentId'");
                $arr[$k]['person'] = $taskInfo['person'];
                $arr[$k]['admin']  = $taskInfo['user'];
            }
        }
        $json['msg']  = "ok";
        $json['data'] = $arr;
        break;

    case "getCommentList":
        $adminUser = $adminInfo['user'];
        $search    = json_decode($data['search'], true);
//        search: {"search":"taskid","searchValue":"柔柔弱弱","filter":{"status":"","goodcomment":""},"pn":1,"total":20924}
        $searchName = $search['search'];

//        $sql = " `type` = '0' ";
        $sql = " `admin` = '$adminUser' ";
        if ($search['searchValue'] !== '') {
            $searchValue = $search['searchValue'];
            $sql         .= "AND `$searchName` = '$searchValue' ";
        }

        $pn  = ($search['pn'] - 1) * 10;
        $sql .= " ORDER BY `id` DESC LIMIT $pn,10";

        $total = $db->getCount('task_good_img', '`id`', $sql);
        $arr   = $db->query("task_good_img", $sql);
        foreach ($arr as $k => $v) {
            $taskid = $v['taskid'];
            $task   = $db->query("task", "`id` = '$taskid'", "Row");

            $arr[$k]['task'] = $task;
        }
        $json['data']   = $arr;
        $json['total']  = $total;
        $json['status'] = "success";
        $json['msg']    = "ok";
        break;
    case "commentStatus":
        $action = $_POST['action'];
        if ($action == 'through') {
            $status         = 1;
            $json['msg']    = "审核通过";
            $json['status'] = "success";
        } else { //$action == 'refuse'
            $status         = 2;
            $json['msg']    = "审核拒绝";
            $json['status'] = "warning";
        }
        $rowId = $_POST["rowId"];
        $sql   = "UPDATE `task_good_img` SET `status` =  '$status' WHERE `id` = '$rowId'";
        $db->execSql($sql);

        break;
    case "supPay":
        $order             = $data['order'];
        $paydb             = MyPDO::getInstance($host, $user, $pass, 'otherdata', 'utf8');
        $date              = date("Y-m-d H:i:s", $time - 60 * 60 * 16);
        $felee_alipay_data = $paydb->query("felee_alipay_data", "`alipay_order` = '$order' AND `entertime` >= '$date'", "Row");

        if (!$felee_alipay_data['aid']) {
            $json['status'] = "error";
            $json['msg']    = "订单号不存在";
            break;
        }
        $note = $felee_alipay_data['memo'];

        preg_match("/收款-(.+?)$/", $note, $memo);
        // print_r($note[1]);
        $note = $memo[1];

        $arr = $db->query("cooperation_billing", "`order` = '$order'", "Row");
        if ($arr['id']) {
            $json['status'] = "error";
            $json['msg']    = "订单号已使用";
            break;
        }
        $pay_money           = $felee_alipay_data['pay_money'];
        $update              = [];
        $update['note']      = '支付宝充值--' . $note;
        $update['change']    = $felee_alipay_data['pay_money'];
        $update['order']     = $felee_alipay_data['alipay_order'];
        $update['user']      = $admin;
        $update['entertime'] = $felee_alipay_data['entertime'];
        $update['money']     = $adminInfo['money'] + $pay_money;
        $db->insert("cooperation_billing", $update);


        $update          = [];
        $update['money'] = $adminInfo['money'] + $pay_money;
        $db->update("cooperation", $update, "`user` = '$admin'");
        $json['status'] = "success";
        $json['msg']    = "补单成功";
        break;
    case  "setxswl":
        $update['xswl'] = $data['money'];

        $users = $db->query("cooperation", "`admin` = '$admin'");
        $sql   = "";
        foreach ($users as $k => $v) {
            $sql .= " '" . $v['user'] . "',";
        }

        $sql = rtrim($sql, ",");
        $db->update("cooperation_price", $update, "`user` = '$admin' OR `user` in ($sql)");


        break;
    case "upBackup3":
        $list = json_decode($data['list'], 1);
        print_r($list);
        $update['day']     = 1;
        $update['babyId']  = $list['babyID'];
        $update['text1']   = $list['text1'];
        $update['img1']    = $data['img1'];
        $update['addTime'] = time();
        $update['up_user'] = $admin;
        $db->insert("backup_new", $update);
        break;
    case "clickWangWang":
        $taobao = $data['wangwang'];
        $arr    = $db->query("users", "`taobao` = '$taobao'", "Row");
        if ($arr['id']) {
            $json['msg']    = '是';
            $json['status'] = 'success';
            break;
        } else {
            $json['msg']    = '不是';
            $json['status'] = 'error';
            break;
        }

        break;
    case "zt":
        $link = $data['link'];

        if ($data['status'] == 1) {
            $db->update("task", ['taskStatus' => 0], "`babyId` = '$link'");
            $db->update("task_queue", ['taskStatus' => 1], "`link` = '$link'");
        } else {
            $db->update("task", ['taskStatus' => 1], "`babyId` = '$link'");
            $db->update("task_queue", ['taskStatus' => 3], "`link` = '$link'");
        }


        break;
    case "uptk":
        $taskListId = $data['taskId'];
        $arr        = $db->query("tklist", "`task_id` = '$taskListId' ", "Row");
        if ($arr['id'] && $arr['status'] != '2') {
            $json['msg']    = '该任务已提交过';
            $json['status'] = 'error';
            break;
        }
        $taskListInfo = $db->query("tasklist", "`id` = '$taskListId'", "Row");
        if ($taskListInfo['addTime'] >= strtotime("2019-01-01")) {
            $json = tk($data, $db, $taskListInfo, $adminInfo, $admin);
            break;
        }
        if ($taskListInfo['addTime'] >= time() - 24 * 60 * 60) {
            $json['msg']    = '只能提交一天后的订单';
            $json['status'] = 'error';
            break;
        }
        if ($taskListInfo['addTime'] <= time() - 7 * 24 * 60 * 60) {
            $json['msg']    = '只能提交7天内订单';
            $json['status'] = 'error';
            break;
        }

        $shop  = $taskListInfo['shop'];
        $order = $taskListInfo['order'];

        $url                     = "http://114.55.144.79/taobao/TradeFullinfoGetRequest.php?shop=$shop&tid=$order&xs=1";
        $xsJson                  = curlGet($url);
        $xsJson                  = json_decode($xsJson, 1);
        $refund_status           = $xsJson['trade']['orders']['order'][0]['refund_status'];
        $update['refund_status'] = getStatus($refund_status);
        $update['money']         = $taskListInfo['principal'];
        if ($taskListInfo['coupons'] != '') {
            $update['money'] = $taskListInfo['couponsMoney'];
        }

        $taskId       = $taskListInfo['taskId'];
        $taskInfo     = $db->query("task", "`id` = '$taskId'", "Row");
        $templateInfo = $taskInfo['template'];
        $templateInfo = json_decode(preg_replace('/($s*$)|(^s*^)|	/m', '', $templateInfo), 1);
        if ($templateInfo['insuranceMoney'] > 0) {
            $update['insurance']      = 1;
            $update['insuranceMoney'] = $templateInfo['insuranceMoney'];
        } else if ($update['money'] >= 150) {
            $json['msg']    = '未买保险，不能提交';
            $json['status'] = 'success';
            break;
        } else if ($update['money'] < 150 && $update['money'] >= 100) {
            $shopInfo = $db->query("cooperation_shop", "`shop` = '$shop'", "Row");
            if ((strtotime(date("Y-m-d H:i:s")) - strtotime($shopInfo['addTime'])) / 86400 > 30) {
                $json['msg']    = '未买保险，不能提交';
                $json['status'] = 'success';
                break;
            }
        } else if ($update['money'] < 100 && $adminInfo['admin'] != '微笑') {
            $json['msg']    = '未买保险，不能提交';
            $json['status'] = 'success';
            break;
        }
        $update['remark']      = $data['remark'];
        $update['img']         = $data['img'];
        $update['refund_time'] = $data['refund_time'];
        $update['task_id']     = $taskListId;
        $update['user']        = $admin;
        $update['addTime']     = date("Y-m-d H:i:s");
        $db->insert("tklist", $update);

        $update              = [];
        $update['complaint'] = 1;
        $db->update("tasklist", $update, "`id` = '$tasklistId'");


        $json['msg']    = '提交成功，等待审核';
        $json['status'] = 'success';
        break;
    case "upnotask":
        $taskId = $data['taskId'];
        $time   = $time - 5 * 24 * 60 * 60;
        $arr    = $db->query("cooperation_pay_task", "`status` = 0 AND `user` = '$admin'  AND `addTime` >= '$time' AND `id` = '$taskId'", "Row");

        if ($arr['id']) {
            $money            = $arr['money'];
            $update           = [];
            $update['status'] = 99;

            $sql = "UPDATE `cooperation` SET `money` = `money` + '$money' WHERE `user` = '$admin'";

            $db->update("cooperation_pay_task", $update, "`id` = '$taskId'");
            $db->execSql($sql);
            $billingUpdate['change']  = $money;
            $billingUpdate['note']    = "任务未发出-$taskId";
            $billingUpdate['addTime'] = date("Y-m-d H:i:s");
            $billingUpdate['user']    = $admin;
            $billingUpdate['money']   = $adminInfo['money'] + $money;
            $db->insert("cooperation_billing", $billingUpdate);
            $json['msg']    = "成功";
            $json['status'] = 'success';
        } else {
            $json['msg']    = "失败";
            $json['status'] = "error";
        }

        break;
    case "notask":

        $time = $time - 5 * 24 * 60 * 60;
        $arr  = $db->query("cooperation_pay_task", "`status` = 0 AND `user` = '$admin'  AND `addTime` >= '$time' ORDER BY `id` DESC");
        foreach ($arr as $k => $v) {
            $arr[$k]['time'] = date("Y-m-d H:i:s", $v['addTime']);
        }

        $json['data'] = $arr;
        break;
    case "complaint":
        $tasklistId          = $data['taskListId'];
        $update['complaint'] = $data['status'];
        $db->update("tasklist", $update, "`id` = '$tasklistId'");
        $json['status'] = "success";
        $json['msg']    = "成功";
        break;
    case "getSjMoney":
        $search     = json_decode($data['search'], true);
        $searchName = $search['search'];
        $sql        = " `user` = '$admin' ";
        if ($search['searchValue'] != '') {
            $searchValue = $search['searchValue'];
            $sql         .= "AND `$searchName` LIKE '%$searchValue%' ";
        }

        $total = $db->getCount('sjMoney', '`id`', $sql);
        $pn    = ($search['pn'] - 1) * 20;
        $sql   .= " ORDER BY `id` DESC LIMIT $pn,20";

        $arr = $db->query("sjMoney", $sql);
        foreach ($arr as $k => $v) {
            $arr[$k]['img'] = json_decode($arr[$k]['img'], 1);
        }
        $json['status'] = "success";
        $json['msg']    = "ok";
        $json['total']  = $total;
        $json['data']   = $arr;
        break;
    case "getRanking":
//        /$json['data']['num'] = 1;
        $shopLists = $db->query("cooperation_shop", "`user` = '$admin'");

        $links   = [];
        $taskIds = [];
        $i       = 0;
        foreach ($shopLists as $k => $v) {
            $shop = $v['shop'];
            // $shopList = $db->query("cooperation_shop", "`user` = '$user' AND (`shop` = '$shop' OR `taobao` = '$shop')", "Row");

            $arr = $db->query("tasklist", "`shop` = '$shop' AND `status` = '1001' AND `addTime` >= '$beginToday2' AND `addTime` <= '$endToday2' ");
            // print_r($arr);

            foreach ($arr as $n => $val) {//任务列表
                $val['coupons'] != '' ? $money = $val['couponsMoney'] : $money = $val['principal'];
                $principal = $val['principal'];  //principal
                $link      = trim($val['babyId']);

                if ($links[$link]) {
                    $json['data'][$links[$link] - 1]['money'] += $money;
                    $json['data'][$links[$link] - 1]['num']++;
                } else {

                    if (!$taskIds[$val['taskId']]) {
                        $taskId                    = $val['taskId'];
                        $count                     = $db->getNum("task", "toNum", "`id` = '$taskId' AND `taskStatus` = '0' AND `addTime` >= '$beginToday2' AND `addTime` <= '$endToday2' ");
                        $json['data'][$i]['count'] = $count;
                        $taskIds[$val['taskId']]   = $i;

                    }
                    $remainingNum = $db->getNum("task_queue", "remainingNum", "`link` = '$link' AND `taskStatus` = '1' AND `taskStartTime` >= '$beginToday2' AND `taskStartTime` <= '$endToday2' ");
                    if ($val['addTime'] >= strtotime(date("Y-m-d H")) && $val['addTime'] <= strtotime(date("Y-m-d H") + 60 * 60)) {
                        $json['data'][$i]['remainingHNum']++;
                    }

                    $json['data'][$i]['remainingNum'] = $remainingNum;

                    $json['data'][$i]['link']      = $link;
                    $json['data'][$i]['name']      = $val['babyName'];
                    $json['data'][$i]['shop']      = $val['shop'];
                    $json['data'][$i]['money']     = $money;
                    $json['data'][$i]['principal'] += $principal;
                    $json['data'][$i]['num']       = 1;


                    $links[$link] = $i + 1;
                    $i++;
                }


            }

        }
        break;
    case "getBackupNum":
        $link = $data['link'];
        $arr  = $db->query("backup_new", "`babyId` = '$link' AND `up_user` = '$admin'");

        $links   = [];
        $y       = [];
        $days    = [];
        $usetime = time() - 30 * 24 * 60 * 60;
        $znum    = 0;
        $ynum    = 0;
        foreach ($arr as $k => $v) {
            $day = $v['day'];
            if (!$days[$day]) {
                $links[$day] = 1;
                $days[$day]  = 1;
                $znum++;
                if ($v['useTime'] > $usetime) {
                    $y[$day] = 1;
                    $ynum++;
                }
            } else {
                $links[$day]++;
                $znum++;
                if ($v['useTime'] > $usetime) {
                    $y[$day]++;
                    $ynum++;
                }
            }
        }
        $json['data']['z']    = $links;
        $json['data']['y']    = $y;
        $json['data']['znum'] = $znum;
        $json['data']['ynum'] = $ynum;
        break;
    case "getPrice":
        $price = $db->query("cooperation_price", "`user` = '$admin'", "Row");
        if (!$price['id']) {
            $price = $db->query("cooperation_price", "`user` = 'adminla'", "Row");
        }
        $price['info'] = json_decode($price['info'], 1);
        $json['data']  = $price['info'];
        $json['xswl']  = $price['xswl'];
        break;
    case "getShopInfo":
        $tid            = $data['babyId'];
        $shop           = $data['shop'];
        $html           = file_get_contents("http://tao.bdeust.cn/taobao/ItemSellerGetRequest.php?shop=$shop&tid=$tid");
        $json['data']   = json_decode($html, 1);
        $json['status'] = 'success';
        break;
    case "payTaskMoney":
        $sqlData = json_decode($data['data'], true);
        if ($sqlData['money'] <= 0) {
            $json['status'] = "error";
            $json['msg']    = "余额不足";
            break;
        }
        //print_r($sqlData);
        $taskNum          = 0;
        $yhqTaskNum       = 0;
        $pjdTaskNum       = 0;
        $sqlData['money'] = $sqlData['money'] * 1;
        foreach ($sqlData['nums'] as $k => $v) {
            foreach ($v['yj']['toNum'] as $value) {
                $taskNum = bcadd($taskNum, $value, 2);
            }
            foreach ($v['yhq']['toNum'] as $value) {
                $yhqTaskNum = bcadd($yhqTaskNum, $value, 2);
            }
            foreach ($v['pjd']['toNum'] as $value) {
                $pjdTaskNum = bcadd($pjdTaskNum, $value, 2);
            }
        }
        $cooperation_price = $db->query("cooperation_price", "`user` = '$admin'", "Row");
        if ($cooperation_price['id']) {
            $cooperation_price_arr = json_decode($cooperation_price['info'], 1);
        } else {
            $cooperation_price     = $db->query("cooperation_price", "`user` = 'adminla'", "Row");
            $cooperation_price_arr = json_decode($cooperation_price['info'], 1);
            $adminla               = 1;
        }
        // $userAdmin = $adminInfo['admin'];
        // $cooperation_prices =  $db->query("cooperation_price", "`user` = '$userAdmin'", "Row");
        $xswlMoney = $cooperation_price['xswl'] * 1;
        switch (true) {
            case $sqlData['money'] <= 100:
                $cooperation_price = $cooperation_price_arr[0];
                break;
            case $sqlData['money'] <= 150:
                $cooperation_price = $cooperation_price_arr[1];
                break;
            case $sqlData['money'] <= 250:
                $cooperation_price = $cooperation_price_arr[2];
                break;
            case $sqlData['money'] <= 350:
                $cooperation_price = $cooperation_price_arr[3];
                break;
            case $sqlData['money'] <= 450:
                $cooperation_price = $cooperation_price_arr[4];
                break;
            case $sqlData['money'] <= 500:
                $cooperation_price = $cooperation_price_arr[5];
                break;
            case $sqlData['money'] <= 800:
                $cooperation_price = $cooperation_price_arr[6];
                break;
            case $sqlData['money'] <= 1000:
                $cooperation_price = $cooperation_price_arr[7];
                break;
        }
        /*if ($adminla) {
            //$cooperation_price += $adminInfo['type'] * 2;
            $cooperation_price += 1;
        }*/
        /* echo $cooperation_price;
         exit();*/
        $pjdUseMoney = bcmul($pjdTaskNum, bcadd($cooperation_price, 6, 2), 2); //评价单金额

        $useMoney = bcadd(bcmul(bcadd($taskNum, $yhqTaskNum, 2), $cooperation_price, 2), bcadd(bcmul($taskNum, $sqlData['money'], 2), bcmul($yhqTaskNum, $sqlData['money2'], 2), 2), 2);

        $useMoney = bcadd($pjdUseMoney, $useMoney, 2);

        if ($sqlData['xswl']) {
            $sxwlNum = ($pjdTaskNum + $taskNum + $yhqTaskNum) * $xswlMoney;

            $useMoney = bcadd($sxwlNum, $useMoney, 2);

        }

        if ($sqlData['insuranceMoney']) {
            $useMoney += $sqlData['insuranceMoney'];
        }
        if ($sqlData['sf1']) {
            $useMoney += ($pjdTaskNum + $taskNum + $yhqTaskNum) * 0.1;
        }
        if ($sqlData['sf2']) {
            $useMoney += ($pjdTaskNum + $taskNum + $yhqTaskNum) * 3;
        }
        if ($sqlData['sf3']) {
            $useMoney += ($pjdTaskNum + $taskNum + $yhqTaskNum) * 1;
        }
        if ($sqlData['sm']) {
            $useMoney += ($pjdTaskNum + $taskNum + $yhqTaskNum) * 0.2;
        }
        if ($sqlData['nl']) {
            $useMoney += ($pjdTaskNum + $taskNum + $yhqTaskNum) * 0.2;
        }
        if ($sqlData['pjdNum']) {
            $useMoney += $sqlData['pjdNum'] * 3;
        }
        $useMoney += $sqlData['addCommentsNum'] * 5;
        if (bccomp($adminInfo['money'], $useMoney, 2) == -1) {
            $json['status'] = "error";
            $json['msg']    = "余额不足";
            break;
        }
        if ($useMoney <= 0) {
            $json['status'] = "error";
            $json['msg']    = "余额不足";
            break;
        }
        $upadminInfo['money'] = bcsub($adminInfo['money'], $useMoney, 2);

        $db->update('cooperation', $upadminInfo, "`user` = '$admin'");
        $updateBilling['change'] = "-$useMoney";
        $updateBilling['user']   = $admin;
        $updateBilling['note']   = "发布任务-" . $sqlData['shop'] . "-" . $sqlData['baobyId'];
        $updateBilling['money']  = $upadminInfo['money'];
        $db->insert('cooperation_billing', $updateBilling);

        $update['user']     = $admin;
        $update['template'] = $data['data'];
        $update['template'] = str_replace("'", "\'", $update['template']);
        $update['addTime']  = time();
        $update['money']    = $useMoney;
        $json['sql']        = $update;

        $db->insert('cooperation_pay_task', $update);

        $json['status'] = "success";
        $json['msg']    = "支付成功";
        break;
    case "supPay2":
        $order = $data['order'];
        $redis = new Redis();
        $redis->connect('127.0.0.1', 6379);
        $redis->auth("qqq123123.");

        if (!$redis->exists("alpayorder-" . $order)) {

            $orders['order'] = $order;
            $orders['user']  = $adminInfo['user'];

            $redis->lPush("alpayorder", json_encode($orders));
            $redis->setex("alpayorder-" . $order, 60 * 60 * 24, 1);
        }
        $json['status'] = "success";
        $json['msg']    = "提交成功等待审核";

        break;
    case "supPay":
//        1.cooperation_billing 存在，已使用
//        2.otherdata.felee_alipay_data 是否存在(挂载软件)，
//        3.是充值，否记录
        $order     = trim($data['order']);  //交易单号
        $prev_date = date("Y-m-d H:i:s", $time - 12 * 60 * 60);

//        @step1-cooperation_billing
        $cooperateBillingArr = $db->query("cooperation_billing", "`order` = '$order'", "Row");
        if (is_array($cooperateBillingArr)) {
            $json['status'] = "error";
            $json['msg']    = "订单号已使用";
            break;
        }
//        @step2-felee_alipay_data
        $paydb             = MyPDO::getInstance($host, $user, $pass, 'otherdata', 'utf8');
        $felee_alipay_data = $paydb->query("felee_alipay_data", "`alipay_order` = '$order' AND `entertime` >= '$prev_date'", "Row");
        if (is_array($felee_alipay_data)) {  //入账（充值）开始补单

            $note = $felee_alipay_data['memo']; //备注内容

            preg_match("/收款-(.+?)$/", $note, $memo);  //收款-CWAJ
            $note      = $memo[1];  //cwaj
            $pay_money = $felee_alipay_data['pay_money'];

            $cooperateBillingData = [
                'note'      => '补单--' . $note,
                'change'    => $felee_alipay_data['pay_money'],
                'order'     => $felee_alipay_data['alipay_order'],
                'user'      => $admin,
                'entertime' => $felee_alipay_data['entertime'],
                'addTime'   => date("Y-m-d H:i:s"),
            ];
            $db->insert("cooperation_billing", $cooperateBillingData);

            $cooperateUpdateData = [
                'money' => $adminInfo['money'] + $pay_money,
            ];
            $db->update("cooperation", $cooperateUpdateData, "`user` = '$admin'");
            $json['status'] = "success";
            $json['msg']    = "补单成功";
        } else {  //未入账（记录ooperation_alipay_log）
            $AlipayLogArr = $db->query("cooperation_alipay_log", "`order` = '$order'", "Row");
            if (!$AlipayLogArr) {
                $AlipayLogData = [
                    'order'              => $order,
                    'cooperation_id'     => $adminInfo['id'],
                    'cooperation_user'   => $adminInfo['user'],
                    'cooperation_status' => $adminInfo['status'],
                    'status'             => 0,
                    'addTime'            => date("Y-m-d H:i:s"),
                ];
                $db->insert("cooperation_alipay_log", $AlipayLogData);
            }
            $json['status'] = "success";
            $json['msg']    = "交易单号，已转至人工审核";
        }
        break;
    case "addPayKey":
        $str    = "mddTYUPASDFGHJKLZXCVBNM";
        $time   = $time - 60 * 30;
        $paykey = substr(str_shuffle($str), 0, 4);
        $arr    = $db->query("cooperation_pay_log", "`paykey` = '$paykey' AND `addTime` > '$time' AND `status` = '0'", "Row");

        while ($arr['id']) {
            $paykey = substr(str_shuffle($str), 0, 4);
            $arr    = $db->query("cooperation_pay_log", "`paykey` = '$paykey' AND `addTime` > '$time' AND `status` = '0'", "Row");
        }

        $update['paykey']  = $paykey;
        $update['addTime'] = time();
        $data['user'] ? $update['user'] = $data['user'] : $update['user'] = $admin;


        $db->insert('cooperation_pay_log', $update);
        $json['msg'] = $update['paykey'];
        break;
    case "setCooperationStatus":
        $cooperationId    = $data['cooperationId'];
        $update['status'] = $data['status'];
        $db->update("cooperation", $update, "`id` = '$cooperationId'");
        $json['status'] = "success";
        $json['msg']    = "成功";
        break;
    case "getCooperationList2":
        $type       = $data['type'];
        $search     = json_decode($data['search'], true);
        $searchName = $search['search'];
        $sql        = " `admin` = '$admin'";
        if ($type != '')
            $sql = " `admin` = '$admin' AND `type` = '$type' ";
        if ($search['searchValue'] !== '') {
            $searchValue = $search['searchValue'];
            $sql         .= "AND `$searchName` LIKE '%$searchValue%' ";
        }

        $total = $db->getCount('cooperation', '`id`', $sql);
        $pn    = ($search['pn'] - 1) * 40;
        $sql   .= " ORDER BY `id` DESC LIMIT $pn,40";

        $arr                          = $db->query("cooperation", $sql);
        $cooperation_price_admin_info = $db->query('cooperation_price', "`user` = 'adminla'", "Row");
        $cooperation_price_admin_arr  = json_decode($cooperation_price_admin_info['info'], 1);
        $shops                        = [];
        foreach ($arr as $key1 => $value1) {
            $userName = $value1['user'];
            $shops1   = $db->query("cooperation_shop", "`user` = '$userName'");
            if (count($shops1) < 1) {
                continue;
            }
            $sql = "  ";
            foreach ($shops1 as $key => $value) {
                $shop = $value['shop'];
                $sql  .= " `shop` = '$shop' OR ";
            }
            $sql      = rtrim($sql, 'OR ');
            $taskList = $db->query("task_queue", " `taskStartTime` < '$endToday2' AND ($sql)");


            foreach ($taskList as $k => $v) {
                $shop   = $v['shop'];
                $link   = $v['link'];
                $taskId = $v['taskId'];
                if (!$shops[$shop] || !$shops[$shop][$link] || !$shops[$shop][$link][$taskId]) {
                    $shopInfo = $db->query("cooperation_shop", "`shop` = '$shop'", "Row");
                    if ($shopInfo['id']) {
//            /echo $shop . PHP_EOL;
                        $user = $shopInfo['user'];


                        $taskInfo = $db->query("task", "`id` = '$taskId'", "Row");

                        $shops[$shop][$link][$taskId]['all']  = $taskInfo['toNum'];
                        $shops[$shop][$link][$taskId]['user'] = $user;
                        //$shops[$shop][$link][$taskId]['tasks'][$taskId] = 1;

                        $cooperation_price_info = $db->query('cooperation_price', "`user` = '$user'", "Row");
                        if ($cooperation_price_info['id']) {
                            $cooperation_price_arr = json_decode($cooperation_price_info['info'], 1);
                        } else {
                            $cooperation_price_arr = json_decode($cooperation_price_admin_info['info'], 1);
                        }


                        if ($taskInfo['coupons']) {
                            $shopMoney = $taskInfo['couponsMoney'];

                        } else {
                            $shopMoney = $taskInfo['money'];
                        }

                        $shops[$shop][$link][$taskId]['shopMoney'] = $shopMoney;
                        $shops[$shop][$link][$taskId]['time']      = $taskInfo['addTime'];
                        switch (true) {
                            case $shops[$shop][$link][$taskId]['shopMoney'] <= 100:
                                $cooperation_price  = $cooperation_price_arr[0];
                                $cooperation_price2 = $cooperation_price_admin_arr[0];
                                break;
                            case $shops[$shop][$link][$taskId]['shopMoney'] <= 150:
                                $cooperation_price  = $cooperation_price_arr[1];
                                $cooperation_price2 = $cooperation_price_admin_arr[1];
                                break;
                            case $shops[$shop][$link][$taskId]['shopMoney'] <= 250:
                                $cooperation_price  = $cooperation_price_arr[2];
                                $cooperation_price2 = $cooperation_price_admin_arr[2];
                                break;
                            case $shops[$shop][$link][$taskId]['shopMoney'] <= 350:
                                $cooperation_price  = $cooperation_price_arr[3];
                                $cooperation_price2 = $cooperation_price_admin_arr[3];
                                break;
                            case $shops[$shop][$link][$taskId]['shopMoney'] <= 450:
                                $cooperation_price  = $cooperation_price_arr[4];
                                $cooperation_price2 = $cooperation_price_admin_arr[4];
                                break;
                            case $shops[$shop][$link][$taskId]['shopMoney'] <= 500:
                                $cooperation_price  = $cooperation_price_arr[5];
                                $cooperation_price2 = $cooperation_price_admin_arr[5];
                                break;
                            case $shops[$shop][$link][$taskId]['shopMoney'] <= 800:
                                $cooperation_price  = $cooperation_price_arr[6];
                                $cooperation_price2 = $cooperation_price_admin_arr[6];
                                break;
                            case $shops[$shop][$link][$taskId]['shopMoney'] <= 1000:
                                $cooperation_price  = $cooperation_price_arr[7];
                                $cooperation_price2 = $cooperation_price_admin_arr[7];
                                break;
                        }
                        $shops[$shop][$link][$taskId]['comMoney']      = $cooperation_price;
                        $shops[$shop][$link][$taskId]['adminComMoney'] = $cooperation_price2;
                    }
                }
            }
        }

        $users = [];
        foreach ($shops as $k => $v) {
            foreach ($v as $key => $value) {
                foreach ($value as $taskId => $info) {
                    if (!$users[$info['user']] || !$users[$info['user']][$k] || !$users[$info['user']][$k][$taskId]) {
                        $okNum                                          = $db->getCount("tasklist", "id", "`status` ='1001' AND  `taskId` = '$taskId'");
                        $users[$info['user']][$k][$taskId]['shopMoney'] = $info['shopMoney'];
                        $users[$info['user']][$k][$taskId]['all']       = $info['all'];
                        $users[$info['user']][$k][$taskId]['comMoney']  = $info['comMoney'];
                        $users[$info['user']][$k][$taskId]['okNum']     = $okNum;
                        $users[$info['user']][$k][$taskId]['noNum']     = ($info['all'] - $okNum);

                        $users[$info['user']][$k]['allMoney']            += $info['shopMoney'] * $okNum;
                        $users[$info['user']][$k]['allComMoney']         += $info['comMoney'] * $okNum;
                        $users[$info['user']][$k]['allOk']               += $okNum;
                        $users[$info['user']][$k]['allNo']               += ($info['all'] - $okNum);
                        $users[$info['user']][$k][$taskId]['bMoney']     = ($info['shopMoney'] + $info['comMoney']) * $users[$info['user']][$k][$taskId]['noNum'];
                        $users[$info['user']][$k][$taskId]['okMoney']    = ($info['shopMoney'] + $info['comMoney']) * $users[$info['user']][$k][$taskId]['okNum'];
                        $users[$info['user']][$k][$taskId]['okComMoney'] = ($info['comMoney']) * $users[$info['user']][$k][$taskId]['okNum'];

                        $users[$info['user']]['okNum']      += $users[$info['user']][$k][$taskId]['okNum'];
                        $users[$info['user']]['bMoney']     += $users[$info['user']][$k][$taskId]['bMoney'];
                        $users[$info['user']]['okMoney']    += $users[$info['user']][$k][$taskId]['okMoney'];
                        $users[$info['user']]['okComMoney'] += $users[$info['user']][$k][$taskId]['okComMoney'];
                        if ($admin == '微笑') {
                            if ($info['user'] == '涛涛' || $info['time'] >= 1540033078) {
                                $users[$info['user']]['tc'] += ($info['comMoney'] - $info['adminComMoney']) * $users[$info['user']][$k][$taskId]['okNum'] * 0.8;
                            } else {
                                $users[$info['user']]['tc'] += ($info['comMoney']) * $users[$info['user']][$k][$taskId]['okNum'] * 0.1;
                            }
                        } else {
                            $users[$info['user']]['tc'] += ($info['comMoney']) * $users[$info['user']][$k][$taskId]['okNum'] * 0.1;
                        }


                    }
                }
            }
        }
        $shops          = [];
        $json['usersz'] = $users;
        foreach ($arr as $key1 => $value1) {
            $userName = $value1['user'];
            $shops1   = $db->query("cooperation_shop", "`user` = '$userName'");


            if (count($shops1) < 1) {
                continue;
            }
            $json['shops'][] = $shops1;
            $sql             = "  ";
            foreach ($shops1 as $key => $value) {
                $shop = $value['shop'];
                $sql  .= " `shop` = '$shop' OR ";
            }
            $sql      = rtrim($sql, 'OR ');
            $taskList = $db->query("task_queue", " `taskStartTime` >= '$beginToday' AND  `taskStartTime` < '$endToday' AND ($sql)");


            foreach ($taskList as $k => $v) {
                $shop   = $v['shop'];
                $link   = $v['link'];
                $taskId = $v['taskId'];
                if (!$shops[$shop] || !$shops[$shop][$link] || !$shops[$shop][$link][$taskId]) {
                    $shopInfo = $db->query("cooperation_shop", "`shop` = '$shop'", "Row");
                    if ($shopInfo['id']) {
//            /echo $shop . PHP_EOL;
                        $user = $shopInfo['user'];


                        $taskInfo = $db->query("task", "`id` = '$taskId'", "Row");

                        $shops[$shop][$link][$taskId]['all']  = $taskInfo['toNum'];
                        $shops[$shop][$link][$taskId]['user'] = $user;
                        //$shops[$shop][$link][$taskId]['tasks'][$taskId] = 1;

                        $cooperation_price_info = $db->query('cooperation_price', "`user` = '$user'", "Row");
                        if ($cooperation_price_info['id']) {
                            $cooperation_price_arr = json_decode($cooperation_price_info['info'], 1);
                        } else {
                            $cooperation_price_arr = json_decode($cooperation_price_admin_info['info'], 1);
                        }


                        if ($taskInfo['coupons']) {
                            $shopMoney = $taskInfo['couponsMoney'];

                        } else {
                            $shopMoney = $taskInfo['money'];
                        }

                        $shops[$shop][$link][$taskId]['shopMoney'] = $shopMoney;

                        switch (true) {
                            case $shops[$shop][$link][$taskId]['shopMoney'] <= 100:
                                $cooperation_price = $cooperation_price_arr[0];
                                break;
                            case $shops[$shop][$link][$taskId]['shopMoney'] <= 150:
                                $cooperation_price = $cooperation_price_arr[1];
                                break;
                            case $shops[$shop][$link][$taskId]['shopMoney'] <= 250:
                                $cooperation_price = $cooperation_price_arr[2];
                                break;
                            case $shops[$shop][$link][$taskId]['shopMoney'] <= 350:
                                $cooperation_price = $cooperation_price_arr[3];
                                break;
                            case $shops[$shop][$link][$taskId]['shopMoney'] <= 450:
                                $cooperation_price = $cooperation_price_arr[4];
                                break;
                            case $shops[$shop][$link][$taskId]['shopMoney'] <= 500:
                                $cooperation_price = $cooperation_price_arr[5];
                                break;
                            case $shops[$shop][$link][$taskId]['shopMoney'] <= 800:
                                $cooperation_price = $cooperation_price_arr[6];
                                break;
                            case $shops[$shop][$link][$taskId]['shopMoney'] <= 1000:
                                $cooperation_price = $cooperation_price_arr[7];
                                break;
                        }
                        $shops[$shop][$link][$taskId]['comMoney'] = $cooperation_price;
                    }
                }
            }
        }

        $json['shop'] = $shops;
        $users        = [];
        foreach ($shops as $k => $v) {
            foreach ($v as $key => $value) {
                foreach ($value as $taskId => $info) {
                    if (!$users[$info['user']] || !$users[$info['user']][$k] || !$users[$info['user']][$k][$taskId]) {
                        $okNum                                          = $db->getCount("tasklist", "id", "`status` ='1001' AND  `taskId` = '$taskId'");
                        $users[$info['user']][$k][$taskId]['shopMoney'] = $info['shopMoney'];
                        $users[$info['user']][$k][$taskId]['all']       = $info['all'];
                        $users[$info['user']][$k][$taskId]['comMoney']  = $info['comMoney'];
                        $users[$info['user']][$k][$taskId]['okNum']     = $okNum;
                        $users[$info['user']][$k][$taskId]['noNum']     = ($info['all'] - $okNum);

                        $users[$info['user']][$k]['allMoney']            += $info['shopMoney'] * $okNum;
                        $users[$info['user']][$k]['allComMoney']         += $info['comMoney'] * $okNum;
                        $users[$info['user']][$k]['allOk']               += $okNum;
                        $users[$info['user']][$k]['allNo']               += ($info['all'] - $okNum);
                        $users[$info['user']][$k][$taskId]['bMoney']     = ($info['shopMoney'] + $info['comMoney']) * $users[$info['user']][$k][$taskId]['noNum'];
                        $users[$info['user']][$k][$taskId]['okMoney']    = ($info['shopMoney'] + $info['comMoney']) * $users[$info['user']][$k][$taskId]['okNum'];
                        $users[$info['user']][$k][$taskId]['okComMoney'] = ($info['comMoney']) * $users[$info['user']][$k][$taskId]['okNum'];

                        $users[$info['user']]['okNum']  += $users[$info['user']][$k][$taskId]['okNum'];
                        $users[$info['user']]['bMoney'] += $users[$info['user']][$k][$taskId]['bMoney'];

                        $users[$info['user']]['okMoney']    += $users[$info['user']][$k][$taskId]['okMoney'];
                        $users[$info['user']]['okComMoney'] += $users[$info['user']][$k][$taskId]['okComMoney'];
                    }
                }
            }
        }


        $json['status'] = "success";
        $json['msg']    = "ok";
        $json['total']  = 0;
        $json['data']   = $arr;

        $json['users'] = $users;
        break;
    case "getCooperationList":
        $type       = $data['type'];
        $search     = json_decode($data['search'], true);
        $searchName = $search['search'];
        $sql        = " `admin` = '$admin'";
        if ($type != '')
            $sql = " `admin` = '$admin' AND `type` = '$type' ";
        if ($search['searchValue'] !== '') {
            $searchValue = $search['searchValue'];
            $sql         .= "AND `$searchName` LIKE '%$searchValue%' ";
        }

        $total = $db->getCount('cooperation', '`id`', $sql);
        $pn    = ($search['pn'] - 1) * 20;
        $sql   .= " ORDER BY `id` DESC LIMIT $pn,20";

        $arr                          = $db->query("cooperation", $sql);
        $cooperation_price_admin_info = $db->query('cooperation_price', "`user` = 'adminla'", "Row");
        foreach ($arr as $key1 => $value1) {
            $userName = $value1['user'];
            $shops    = $db->query("cooperation_shop", "`user` = '$userName'");
            if (count($shops) < 1) {
                continue;
            }
            $sql = "  ";
            foreach ($shops as $key => $value) {
                $shop = $value['shop'];
                $sql  .= " `shop` = '$shop' OR ";
            }
            $sql      = rtrim($sql, 'OR ');
            $taskList = $db->query("task_queue", "`taskStartTime` >= '$beginToday' AND `taskStartTime` < '$endToday' AND ($sql)");
            $shops    = [];
            foreach ($taskList as $k => $v) {
                $shop   = $v['shop'];
                $link   = $v['link'];
                $taskId = $v['taskId'];
                if (!$shops[$shop] || !$shops[$shop][$link] || !$shops[$shop][$link][$taskId]) {
                    $shopInfo = $db->query("cooperation_shop", "`shop` = '$shop'", "Row");
                    if ($shopInfo['id']) {
//            /echo $shop . PHP_EOL;
                        $user = $shopInfo['user'];


                        $taskInfo = $db->query("task", "`id` = '$taskId'", "Row");

                        $shops[$shop][$link][$taskId]['all']  = $taskInfo['toNum'];
                        $shops[$shop][$link][$taskId]['user'] = $user;
                        //$shops[$shop][$link][$taskId]['tasks'][$taskId] = 1;

                        $cooperation_price_info = $db->query('cooperation_price', "`user` = '$user'", "Row");
                        if ($cooperation_price_info['id']) {
                            $cooperation_price_arr = json_decode($cooperation_price_info['info'], 1);
                        } else {
                            $cooperation_price_arr = json_decode($cooperation_price_admin_info['info'], 1);
                        }


                        if ($taskInfo['coupons']) {
                            $shopMoney = $taskInfo['couponsMoney'];

                        } else {
                            $shopMoney = $taskInfo['money'];
                        }

                        $shops[$shop][$link][$taskId]['shopMoney'] = $shopMoney;

                        switch (true) {
                            case $shops[$shop][$link][$taskId]['shopMoney'] <= 100:
                                $cooperation_price = $cooperation_price_arr[0];
                                break;
                            case $shops[$shop][$link][$taskId]['shopMoney'] <= 150:
                                $cooperation_price = $cooperation_price_arr[1];
                                break;
                            case $shops[$shop][$link][$taskId]['shopMoney'] <= 250:
                                $cooperation_price = $cooperation_price_arr[2];
                                break;
                            case $shops[$shop][$link][$taskId]['shopMoney'] <= 350:
                                $cooperation_price = $cooperation_price_arr[3];
                                break;
                            case $shops[$shop][$link][$taskId]['shopMoney'] <= 450:
                                $cooperation_price = $cooperation_price_arr[4];
                                break;
                            case $shops[$shop][$link][$taskId]['shopMoney'] <= 500:
                                $cooperation_price = $cooperation_price_arr[5];
                                break;
                            case $shops[$shop][$link][$taskId]['shopMoney'] <= 800:
                                $cooperation_price = $cooperation_price_arr[6];
                                break;
                            case $shops[$shop][$link][$taskId]['shopMoney'] <= 1000:
                                $cooperation_price = $cooperation_price_arr[7];
                                break;
                        }
                        $shops[$shop][$link][$taskId]['comMoney'] = $cooperation_price;
                    }
                }
            }
        }


        $users = [];
        foreach ($shops as $k => $v) {
            foreach ($v as $key => $value) {
                foreach ($value as $taskId => $info) {
                    if (!$users[$info['user']] || !$users[$info['user']][$k] || !$users[$info['user']][$k][$taskId]) {
                        $okNum                                          = $db->getCount("tasklist", "id", "`status` ='1001' AND  `taskId` = '$taskId'");
                        $users[$info['user']][$k][$taskId]['shopMoney'] = $info['shopMoney'];
                        $users[$info['user']][$k][$taskId]['all']       = $info['all'];
                        $users[$info['user']][$k][$taskId]['comMoney']  = $info['comMoney'];
                        $users[$info['user']][$k][$taskId]['okNum']     = $okNum;
                        $users[$info['user']][$k][$taskId]['noNum']     = ($info['all'] - $okNum);

                        $users[$info['user']][$k]['allMoney']            += $info['shopMoney'] * $okNum;
                        $users[$info['user']][$k]['allComMoney']         += $info['comMoney'] * $okNum;
                        $users[$info['user']][$k]['allOk']               += $okNum;
                        $users[$info['user']][$k]['allNo']               += ($info['all'] - $okNum);
                        $users[$info['user']][$k][$taskId]['bMoney']     = ($info['shopMoney'] + $info['comMoney']) * $users[$info['user']][$k][$taskId]['noNum'];
                        $users[$info['user']][$k][$taskId]['okMoney']    = ($info['shopMoney'] + $info['comMoney']) * $users[$info['user']][$k][$taskId]['okNum'];
                        $users[$info['user']][$k][$taskId]['okComMoney'] = ($info['comMoney']) * $users[$info['user']][$k][$taskId]['okNum'];

                        $users[$info['user']]['bMoney'] += $users[$info['user']][$k][$taskId]['bMoney'];

                        $users[$info['user']]['okMoney']    += $users[$info['user']][$k][$taskId]['okMoney'];
                        $users[$info['user']]['okComMoney'] += $users[$info['user']][$k][$taskId]['okComMoney'];
                    }
                }
            }
        }

        $json['status'] = "success";
        $json['msg']    = "ok";
        $json['total']  = $total;
        $json['data']   = $arr;
        $json['users']  = $users;
        break;
    case "addLink":
        $update['type'] = $data['type'];
        $update['user'] = $admin;
        $update['key']  = md5(time() . uniqid(mt_rand(10000, 99999), 1));
        $db->insert("invite_list", $update);
        $json['status'] = "success";
        break;
    case "getLink":

        for ($i = 0; $i < 3; $i++) {
            $keyList = $db->query("invite_list", "`type` = '$i' AND `user` = '$admin'", "Row");
            if ($keyList['id']) {
                $json['data'][$i]['status'] = 'success';
                $json['data'][$i]['key']    = substr($keyList['key'], 0, 9);
            } else {
                $json['data'][$i]['status'] = 'success';
                $update['type']             = $i;
                $update['user']             = $admin;
                $update['key']              = md5(time() . uniqid(mt_rand(10000, 99999), 1));
                $json['data'][$i]['key']    = $update['key'];
                $db->insert("invite_list", $update);

            }
        }

        /* if ($adminInfo['linkStatus']) {
             $json['status'] = "success";
             $json['key'] = substr($adminInfo['md5'], 0, 6);

             break;
         }
         $json['status'] = "error";*/
        break;
    case "upCommission":
        $sqlData  = json_decode($data['data'], true);
        $userName = $data['user'];
        $arr      = $db->query("cooperation_price", "`user` = '$admin'", "Row");
        if (!$arr['id']) {
            $arr = $db->query("cooperation_price", "`user` = 'adminla'", "Row");
        }


        $arr = json_decode($arr['info'], true);
        foreach ($sqlData as $k => $v) {
            if ($v < $arr[$k] + 2) {
                $json['status'] = "error";
                $json['msg']    = "不能小于成本价";
                $s              = true;
                break;
            }
        }
        if ($s) {
            break;
        }
        if ($data['money'] < 500) {
            $json['status'] = "error";
            $json['msg']    = "接口费不能小于500";
            break;
        }
        $arr = $db->query("cooperation_price", "`user` = '$userName'", "Row");

        $update['info']  = $data['data'];
        $update['money'] = $data['money'];
        $update['user']  = $userName;
        if ($arr['id']) {
            $cooperation_price_id = $arr['id'];
            $db->update("cooperation_price", $update, "`id` = '$cooperation_price_id'");
        } else {
            $db->insert("cooperation_price", $update);
        }
        $json['status'] = "success";
        $json['msg']    = "成功";
        break;
    case "getCommission":
        $arr = $db->query("cooperation_price", "`user` = '$admin'", "Row");
        if (!$arr['id']) {
            $arr = $db->query("cooperation_price", "`user` = 'adminla'", "Row");
        }
        $arr['info'] = json_decode($arr['info'], 1);
        $userAdmin   = $adminInfo['admin'];
        $arrs        = $db->query("cooperation_price", "`user` = '$userAdmin'", "Row");

        /* if ($adminInfo['type'] != 0) {
             foreach ($arr['info'] as $k => $v) {
                 $arr['info'][$k] = $v + 1;
             }
         }*/
        if ($adminInfo['first_use'] == 0) {
            $arr['money'] = 1;
        }
        $json['data']  = $arr['info'];
        $json['money'] = $arr['money'];
        $json['xswl']  = $arrs['xswl'];
        break;
    case "reSettingPass":
        $sqlData = json_decode($data['data'], true);
        if ($sqlData['pass1'] != $adminInfo['password']) {
            $json['status'] = "error";
            $json['msg']    = "原密码不正确";
            break;
        }
        if ($sqlData['pass'] != $sqlData['checkPass']) {
            $json['status'] = "error";
            $json['msg']    = "两次密码不一致";
            break;
        }
        if (strlen($sqlData['pass']) < 6) {
            $json['status'] = "error";
            $json['msg']    = "密码不能小于6位";
            break;
        }
        $update['password'] = $sqlData['pass'];
        $db->update("cooperation", $update, "`user` = '$admin'");
        $json['status'] = "success";
        $json['msg']    = "修改成功";
        break;
    case "getShopList":
        $arr          = $db->query("cooperation_shop", "`user` = '$admin'");
        $json['data'] = $arr;
        break;
    case "addShop":
        if ($adminInfo['admin'] == '好客分享' || $adminInfo['admin'] == '刘美军') {
            $json['status'] = "error";
            $json['msg']    = "代理商不能绑定店铺";
            break;
        }
        $arr = $db->query("cooperation_shop", "`user` = '$admin'");
        if (count($arr) >= 2) {
            //$json['status'] = "error";
            // $json['msg']    = "最多可绑定2个店铺";
            //break;
        }
        $cooperation_user  = $admin;
        $cooperation_price = $db->query("cooperation_price", "`user` = '$cooperation_user'", "Row");
        $day               = 30;
        if ($adminInfo['first_use'] == 0) {
            $cooperation_money    = 1;
            $update['first_use']  = 1;
            $update['first_time'] = $time;
            $day                  = 7;
        } else {
            if ($cooperation_price['id']) {
                $cooperation_money = $cooperation_price['money'];
            } else {
                $cooperation_money = 500;
            }
            $cooperation_money = 0;
            if ($adminInfo['money'] < $cooperation_money) {
                $json['status'] = "error";
                $json['msg']    = "账户余额不足";
                break;
            }
        }

        $update['money'] = $adminInfo['money'] - $cooperation_money;
        $db->update("cooperation", $update, "`user` = '$admin'");
        $sqlData          = json_decode($data['data'], true);
        $update           = [];
        $update['shop']   = trim($sqlData['shop']);
        $update['taobao'] = trim($sqlData['taobao']);
        $update['user']   = $admin;
        $update['day']    = $day;
        $db->insert("cooperation_shop", $update);
        $updateBilling['change'] = "-$cooperation_money";
        $updateBilling['user']   = $admin;
        $updateBilling['note']   = "绑定店铺";
        $updateBilling['money']  = $update['money'];
        $db->insert('cooperation_billing', $updateBilling);
        $json['status'] = "success";
        $json['msg']    = "添加成功";
        break;
    case "getCooperationBilling":

        $search        = json_decode($data['search'], true);
        $pn            = ($search['pn'] - 1) * 20;
        $sql           = "`user` = '$admin'  ";
        $total         = $db->getCount('cooperation_billing', '`id`', $sql);
        $sql           .= "ORDER BY `id` DESC LIMIT $pn,20";
        $arr           = $db->query("cooperation_billing", "`user` = '$admin' ORDER BY `id` DESC LIMIT $pn,20");
        $json['data']  = $arr;
        $json['total'] = $total;
        $json['pn']    = $search['pn'];
        break;
    case  "getUserInfo":
        $json['data'] = $adminInfo;
        break;
    case "setTitle":
        if ($adminInfo['info.set_title_num'] == 0) {
            $update['title']         = $data['title'];
            $update['set_title_num'] = 1;
            $db->update("cooperation", $update, "`user` = '$admin'");
        }

        break;
    case "getInfo":
        $arr          = $db->query("cooperation", "`user` = '$admin'", "Row");
        $json['data'] = $arr;
        break;
    case "getTemplate":
        $name         = $data['name'];
        $templateInfo = $db->query("task_template", "`name` = '$name'", "Row");
        $arr          = json_decode($templateInfo['text'], 1);
        $json['data'] = $arr;
        break;
    case "getTemplateList":
        $name         = $admin;
        $templateInfo = $db->query("task_template", "`cooperate_user` = '$name'", "All");
        foreach ($templateInfo as $k => $v) {
            $templateInfo[$k]['text'] = json_decode($v['text']);
        }
        $json['data'] = $templateInfo;
        break;
    case "addTemplate":
        $name           = $data['name'];
        $update['name'] = $name;
        $update['text'] = $data['data'];

        $update['user']    = $admin;
        $update['addTime'] = $time;
        $templateInfo      = $db->query("task_template", "`name` = '$name'", "Row");
        if ($templateInfo['id']) {
            $db->update("task_template", $update, "`name` = '$name'");
        } else {
            $db->insert("task_template", $update, "`name` = '$name'");
        }
        break;
    case "getText":
        $arr             = $db->query("test", "`state` = '0'", "Row");
        $tid             = $arr['id'];
        $json['text']    = $arr['text'];
        $update['state'] = 1;
        $db->update("test", $update, "`id` = '$tid'");
        break;
    case "getHelpList":
        $search     = json_decode($data['search'], true);
        $searchName = $search['search'];
        $sql        = ' 1 ';
        if ($search['searchValue'] !== '') {
            $searchValue = $search['searchValue'];
            $sql         .= "AND `$searchName` LIKE '%$searchValue%' ";
        }

        if ($search['filter']['status'] !== '' && isset($search['filter']['status'])) {
            $status = $search['filter']['status'];
            $sql    .= " AND `status` = '$status' ";
        }
        if ($search['filter']['order'] !== '' && isset($search['filter']['order'])) {
            $order = $search['filter']['order'];
            $sql   .= " AND `order` LIKE '%$order%' ";
        }
        if ($search['filter']['explain'] !== '' && isset($search['filter']['explain'])) {
            $order = $search['filter']['explain'];
            $sql   .= " AND `explain` LIKE '%$order%' ";
        }
        $total = $db->getCount('helplist', '`id`', $sql);
        $pn    = ($search['pn'] - 1) * 20;
        $sql   .= " ORDER BY `id` DESC LIMIT $pn,20";

        $arr            = $db->query("helplist", $sql);
        $json['status'] = "success";
        $json['msg']    = "ok";
        $json['total']  = $total;
        $json['data']   = $arr;
        break;
    case "freeze":
        if ($admin != '林杉杉') {

            break;
        }
        $username             = $data['user'];
        $update['userStatus'] = "4";

        $db->update('users', $update, "`user` = '$username'");

        break;
    case "getWechatUserInfo":
        $code         = $data['code'];
        $url          = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$appsecret&code=$code&grant_type=authorization_code";
        $json         = file_get_contents($url);
        $arr          = json_decode($json, true);
        $access_token = $arr['access_token'];
        $openid       = $arr['openid'];

        $text           = file_get_contents("https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN");
        $arr            = json_decode($text, true);
        $openid         = $arr['openid'];
        $arr['bidbing'] = true;
        if (!$db->query("users", "`openid` = '$openid'")) {
            $arr['binding'] = false;
        }
        $json           = [];
        $json['status'] = "success";
        $json['msg']    = "ok";
        $json['data']   = $arr;
        break;
    case "updateUser":
        print_r($_POST);
        print_r(json_decode($_POST['data'], true));
        break;
    case "getPhoneList":
        $search     = json_decode($data['search'], true);
        $searchName = $search['search'];
        $sql        = ' 1 ';
        if ($search['searchValue'] !== '') {
            $searchValue = $search['searchValue'];
            $sql         .= "AND `$searchName` = '$searchValue' ";
        }
        $total         = $db->getCount('phonelist', '`id`', $sql);
        $pn            = ($search['pn'] - 1) * 20;
        $sql           .= " ORDER BY `id` DESC LIMIT $pn,20";
        $arr           = $db->query("phonelist", $sql);
        $json['msg']   = "ok";
        $json['total'] = $total;
        $json['data']  = $arr;
        break;
    case "swPhoneStatus":


        $user = $data['user'];
        $arr  = $db->query("phonelist", "`id` = '$user'", "Row");


        $money = $data['value'];
        if ($adminInfo['balance'] < $money) {
            $json['status'] = "error";
            $json['msg']    = "余额不足";
            break;
        }

        if ($money > 20 || $money < 0) {
            if ($admin != '林杉杉') {
                $json['status'] = "error";
                $json['msg']    = "金额不能大于20";
                break;
            } else if ($money > 500 || $money < 0) {
                $json['status'] = "error";
                $json['msg']    = "金额不能大于500";
                break;
            }
        }
        $openId     = $arr['openid'];      //获取openid
        $outTradeNo = $time . uniqid();     //订单号
        $payAmount  = $money;             //转账金额，单位:元。转账最小金额为1元
        $trueName   = '';         //收款人真实姓名
        $result     = $wxPay->createJsBizPackage($openId, $payAmount, $outTradeNo, $trueName, $db);
        if ($result !== true) {
            $json['status'] = 'error';
            $json['msg']    = '转账失败';
            file_put_contents("log\wxpayUser.log", $openid . " " . $money . "\r\n", FILE_APPEND);
            break;
        } else {

            $date['status']  = 1;
            $date['orderId'] = $outTradeNo;
        }
        $date['explain']        = '收米吧打款';
        $date['text']           = $data['text'];
        $date['user']           = $arr['phone'];
        $date['name']           = $arr['name'];
        $date['openid']         = $arr['openid'];
        $date['addTime']        = $time;
        $date['change']         = '-' . $money;
        $date['admin']          = $_COOKIE['admin'];
        $date['money']          = $adminInfo['balance'] - $money;
        $status                 = $db->insert('billing', $date);
        $updateAdmin['balance'] = $adminInfo['balance'] - $money;
        $adminUser              = $adminInfo['user'];
        //$update['money'] = $money;
        $db->update('admin', $updateAdmin, "`user` = '$adminUser'");

        $update['user']   = $_COOKIE['admin'];
        $update['status'] = $data['status'];
        $status           = $db->update('phonelist', $update, "`id` = '$user'");
        addLog($db, "收米吧,转账成功", $arr['phone'], $update, []);
        $json['status'] = 'success';
        $json['msg']    = '转账成功';

        break;
    case "getPhoneLogList":
        $search     = json_decode($data['search'], true);
        $searchName = $search['search'];
        $sql        = ' 1 ';
        if ($search['searchValue'] !== '') {
            $searchValue = $search['searchValue'];
            $sql         .= "AND `$searchName` = '$searchValue' ";
        }
        $total         = $db->getCount('phonelog', '`id`', $sql);
        $pn            = ($search['pn'] - 1) * 20;
        $sql           .= " ORDER BY `id` DESC LIMIT $pn,20";
        $arr           = $db->query("phonelog", $sql);
        $json['msg']   = "ok";
        $json['total'] = $total;
        $json['data']  = $arr;
        break;
    case "getPhoneUserList":
        $search     = json_decode($data['search'], true);
        $searchName = $search['search'];
        $sql        = ' 1 ';
        if ($search['searchValue'] !== '') {
            $searchValue = $search['searchValue'];
            $sql         .= "AND `$searchName` = '$searchValue' ";
        }
        $sql           .= " AND `balance` > '0' AND `note` != '离职' ";
        $total         = $db->getCount('admin', '`id`', $sql);
        $pn            = ($search['pn'] - 1) * 20;
        $sql           .= " ORDER BY `id` DESC LIMIT $pn,20";
        $arr           = $db->query("admin", $sql);
        $json['msg']   = "ok";
        $json['total'] = $total;
        $json['data']  = $arr;
        break;
}
switch ($id) {
    case "getGoodCommentList":
        $arr = $db->query("goodcomment", " 1 ORDER BY `id` DESC ");
        foreach ($arr as $k => $v) {
            if ($v['person'] == '') {
                $order            = $v['order'];
                $taskListInfo     = $db->query('tasklist', "`order` = '$order'", "Row");
                $taskId           = $taskListInfo['taskId'];
                $taskInfo         = $db->query('task', "`id` = '$taskId'", "Row");
                $update['person'] = $taskInfo['person'];
                $update['admin']  = $taskInfo['user'];
                $goodcommentId    = $v['id'];
                $db->update('goodcomment', $update, "`id` = '$goodcommentId'");
                $arr[$k]['person'] = $taskInfo['person'];
                $arr[$k]['admin']  = $taskInfo['user'];
            }
        }
        $json['msg']  = "ok";
        $json['data'] = $arr;
        break;
    case "swStatus":
        $user     = $data['user'];
        $arr      = $db->query("goodcomment", "`id` = '$user'", "Row");
        $username = $arr['user'];

        if ($arr['status'] == '0' && $data['status'] == '1') {
            $openId     = $arr['openid'];      //获取openid
            $outTradeNo = uniqid();     //订单号
            $payAmount  = 2;             //转账金额，单位:元。转账最小金额为1元
            $trueName   = '';         //收款人真实姓名
            $result     = $wxPay->createJsBizPackage($openId, $payAmount, $outTradeNo, $trueName, $db);
            if ($result !== true) {
                $json['status'] = 'error';
                $json['msg']    = '转账失败';
                break;
            }

            $date['status']  = 1;
            $date['explain'] = '好评返现';
            $date['user']    = $username;
            $date['openid']  = $openid;
            $date['addTime'] = $time;
            $date['change']  = '-' . $payAmount;
            $date['admin']   = $_COOKIE['admin'];
            $date['money']   = $adminInfo['balance'] - $payAmount;
            $status          = $db->insert('billing', $date);

            $updateAdmin['balance'] = $adminInfo['balance'] - $payAmount;
            $db->update('admin', $updateAdmin, "`user` = '$adminUser'");

            $update['status'] = '1001';
            $db->update('goodcomment', $update, "`id` = '$user'");
        }

        if ($arr['status'] == '1001' && $data['status'] == '1') {
            $openId     = $arr['openid'];      //获取openid
            $outTradeNo = uniqid();     //订单号
            $payAmount  = 2;             //转账金额，单位:元。转账最小金额为1元
            $trueName   = '';         //收款人真实姓名
            $result     = $wxPay->createJsBizPackage($openId, $payAmount, $outTradeNo, $trueName, $db);
            if ($result !== true) {
                $json['status'] = 'error';
                $json['msg']    = '转账失败';
                break;
            }
            $date['status']  = 1;
            $date['explain'] = '好评返现';
            $date['user']    = $username;
            $date['openid']  = $openid;
            $date['addTime'] = $time;
            $date['change']  = '-' . $payAmount;
            $date['admin']   = $_COOKIE['admin'];
            $date['money']   = $adminInfo['balance'] - $payAmount;
            $status          = $db->insert('billing', $date);

            $updateAdmin['balance'] = $adminInfo['balance'] - $payAmount;
            $db->update('admin', $updateAdmin, "`user` = '$adminUser'");
            $update['status'] = '2001';
            $db->update('goodcomment', $update, "`id` = '$user'");
            $updateuser['goodcomment'] = '';
            $db->update('users', $updateuser, "`user` = '$username'");
        }

        if ($arr['status'] == '0' && $data['status'] == '2') {
            $update['status'] = '1002';
            $db->update('goodcomment', $update, "`id` = '$user'");
            $updateuser['goodcomment'] = '';
            $db->update('users', $updateuser, "`user` = '$username'");
            $json['status'] = 'success';
            $json['msg']    = '成功';
        }
        if ($arr['status'] == '1002' && $data['status'] == '2') {
            $update['status'] = '2002';
            $db->update('goodcomment', $update, "`id` = '$user'");
            $updateuser['goodcomment'] = '';
            $db->update('users', $updateuser, "`user` = '$username'");
            $json['status'] = 'success';
            $json['msg']    = '成功';
        }
        break;
}
//用户管理
switch ($id) {
    case "getUserList":

        $search     = json_decode($data['search'], true);
        $searchName = $search['search'];
        $sql        = ' 1 ';
        if ($search['searchValue'] !== '') {
            $searchValue = $search['searchValue'];
            $sql         .= "AND `$searchName` = '$searchValue' ";
        }

        if ($search['filter']['user'] !== '' && isset($search['filter']['user'])) {
            $userStatus = $search['filter']['user'];
            $sql        .= " AND `userStatus` = '$userStatus' ";
        }
        if ($search['filter']['task'] !== '' && isset($search['filter']['task'])) {
            $taskStatus = $search['filter']['task'];
            $sql        .= " AND `taskStatus` = '$taskStatus' ";
        }

        $total = $db->getCount('users', '`id`', $sql);
        $pn    = ($search['pn'] - 1) * 20;
        $sql   .= " ORDER BY `id` DESC LIMIT $pn,20";
        $arr   = $db->query("users", $sql);
        foreach ($arr as $k => $v) {

            /*if ($v['city'] == '') {
                $url = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=' . $v['ip'];
                $text = file_get_contents($url);
                $text = json_decode($text, true);
                $arr[$k]['city'] = $text['province'] . " " . $text['city'];
                $update['city'] = $arr[$k]['city'];
                $userid = $v['id'];
                $db->update('users', $update, "`id` = '$userid'");
            }*/
            $time3Day            = $time - 60 * 60 * 24 * 30;
            $userName            = $v['user'];
            $arr[$k]['monthNum'] = $db->getNum('tasklist', "`user` = '$userName' AND `status` ='1001' AND `addTime` > '$time3Day'");
            $arr[$k]['zNum']     = $db->getNum('tasklist', "`user` = '$userName' AND `status` ='1001'");
            $arr[$k]['userGroup'] ? $arr[$k]['userGroup'] = strtoupper($v['userGroup']) . "组" : $arr[$k]['userGroup'] = '暂无';
        }
        $json['status'] = "success";
        $json['total']  = $total;
        $json['msg']    = "ok";
        $json['data']   = $arr;
        break;
    case "addUser":
        $sqlData             = json_decode($data['data'], true);
        $sqlData['birthday'] = substr($sqlData['birthday'], 0, -3);
        $type                = $data['type'];
        unset($sqlData['userGroup']);
        if ($type == "0") {
            $sqlData['addTime'] = time();
            $status             = $db->insert('users', $sqlData);
        } else {
            unset($sqlData['id']);
            $arr['user']     = $sqlData['user'];
            $arr['phone']    = $sqlData['phone'];
            $arr['password'] = $sqlData['password'];
            $arr['taobao']   = $sqlData['taobao'];
            $arr['taobao2']  = $sqlData['taobao2'];
            $status          = $db->update('users', $arr, "`user` = '$type'");
        }
        $json = sedMsg($status);
        break;
    case "dedleteUser":
        $user = $data['user'];


        $status = $db->delete('users', "`user` = '$user'");
        $json   = sedMsg($status);
        break;
    case 'updateUserStatus':
        $user = $data['user'];
        $arr  = $db->query("users", "`user` = '$user'", "Row");
        if ($arr['userStatus'] == '0' && $data['status'] == '1' && $arr['role'] == '1') {
            //师傅审核通过
            $update['activateTime'] = $time;
            $update['userGroup']    = $adminInfo['groups'];
        }
        if ($arr['adminUser'] == '') {
            $update['adminUser'] = $adminInfo['user'];
        }
        $update['admin']      = $adminInfo['user'];
        $update['userStatus'] = $data['status'];
        $update['text']       = $data['text'];
        $status               = $db->update('users', $update, "`user` = '$user'");
        $json                 = sedMsg($status);
        $json['data']         = $update;
        $json['admin']        = $adminInfo;
        break;
}

//任务管理
switch ($id) {
    case "getTaskList":

        $search     = json_decode($data['search'], true);
        $searchName = $search['search'];
        $sql        = " `cooperation` = '$admin' ";
        if ($search['searchValue'] !== '') {
            $searchValue = $search['searchValue'];
            $sql         .= "AND `$searchName` = '$searchValue' ";
        }

        if ($search['filter']['taskStatus'] !== '') {
            $taskStatus = $search['filter']['taskStatus'];
            $sql        .= " AND `taskStatus` = '$taskStatus' ";
        }

        $total = $db->getCount('task', '`id`', $sql);
        $pn    = ($search['pn'] - 1) * 20;
        $sql   .= " ORDER BY `id` DESC LIMIT $pn,20";
        $arr   = $db->query("task", $sql);
        foreach ($arr as $k => $v) {
            $arr[$k]['goods2money'] = json_decode($v['goods2money'], true);
            $arr[$k]['template']    = json_decode($v['template'], true);
            $arr[$k]['userGroup'] ? $arr[$k]['userGroup'] = strtoupper($v['userGroup']) . "组" : $arr[$k]['userGroup'] = '暂无';
        }
        $json['status'] = "success";
        $json['msg']    = "ok";
        $json['total']  = $total;
        $json['data']   = $arr;
        break;
    case "addTask2":

        if ($admin == '商家测试') {
            $groups = ["aaa", "aaa", "aaa"];
        } else {
            $groups = ["d", "b-1", "c-1", "d-1"];
        }


        $sqlData         = json_decode($data['data'], true);
        $sqlData['shop'] = str_replace("'", "\'", $sqlData['shop']);
        //print_r($sqlData);

        $taskNum    = 0;
        $yhqTaskNum = 0;
        foreach ($sqlData['nums'] as $k => $v) {
            foreach ($v['yj']['toNum'] as $value) {
                $taskNum = bcadd($taskNum, $value);
            }
            foreach ($v['yhq']['toNum'] as $value) {
                $yhqTaskNum = bcadd($yhqTaskNum, $value);
            }
            foreach ($v['pjd']['toNum'] as $value) {
                $pjdTaskNum = bcadd($pjdTaskNum, $value);
            }
        }


        $cooperation_price = $db->query("cooperation_price", "`user` = '$admin'", "Row");
        if ($cooperation_price['id']) {
            $cooperation_price_arr = json_decode($cooperation_price['info'], 1);
        } else {
            $cooperation_price     = $db->query("cooperation_price", "`user` = 'adminla'", "Row");
            $cooperation_price_arr = json_decode($cooperation_price['info'], 1);
            $adminla               = 1;
        }
        switch (true) {
            case $sqlData['money'] <= 100:
                $cooperation_price = $cooperation_price_arr[0];
                break;
            case $sqlData['money'] <= 150:
                $cooperation_price = $cooperation_price_arr[1];
                break;
            case $sqlData['money'] <= 250:
                $cooperation_price = $cooperation_price_arr[2];
                break;
            case $sqlData['money'] <= 350:
                $cooperation_price = $cooperation_price_arr[3];
                break;
            case $sqlData['money'] <= 450:
                $cooperation_price = $cooperation_price_arr[4];
                break;
            case $sqlData['money'] <= 500:
                $cooperation_price = $cooperation_price_arr[5];
                break;
            case $sqlData['money'] <= 800:
                $cooperation_price = $cooperation_price_arr[6];
                break;
            case $sqlData['money'] <= 1000:
                $cooperation_price = $cooperation_price_arr[7];
                break;
        }

        $pjdUseMoney = bcmul($pjdTaskNum, bcadd($cooperation_price, 6));

        $useMoney = bcadd(bcmul(bcadd($taskNum, $yhqTaskNum), $cooperation_price), bcadd(bcmul($taskNum, $sqlData['money']), bcmul($yhqTaskNum, $sqlData['money2'])));
        if ($sqlData['xswl']) {
            $sxwlNum  = $pjdTaskNum + $taskNum + $yhqTaskNum;
            $useMoney = bcadd($sxwlNum, $useMoney);

        }
        $useMoney = bcadd($pjdUseMoney, $useMoney);
        //$useMoney = bcadd(bcmul(bcadd($taskNum, $yhqTaskNum), $cooperation_price), bcadd(bcmul($taskNum, $sqlData['money']), bcmul($yhqTaskNum, $sqlData['money2'])));
        if ($sqlData['insuranceMoney']) {
            $useMoney += $sqlData['insuranceMoney'];
        }
        if ($sqlData['sf1']) {
            $useMoney += ($pjdTaskNum + $taskNum + $yhqTaskNum) * 0.1;
        }
        if ($sqlData['sf2']) {
            $useMoney += ($pjdTaskNum + $taskNum + $yhqTaskNum) * 3;
        }
        if ($sqlData['sf3']) {
            $useMoney += ($pjdTaskNum + $taskNum + $yhqTaskNum) * 1;
        }
        if ($sqlData['sm']) {
            $useMoney += ($pjdTaskNum + $taskNum + $yhqTaskNum) * 0.2;
        }
        if ($sqlData['nl']) {
            $useMoney += ($pjdTaskNum + $taskNum + $yhqTaskNum) * 0.2;
        }
        if ($sqlData['pjdNum']) {
            $useMoney += $sqlData['pjdNum'] * 3;
        }
        $useMoney          = sprintf("%.2f", substr(sprintf("%.3f", $useMoney), 0, -2));
        $task_pay          = $db->query("cooperation_pay_task", "`user` = '$admin' AND `status` = 0  ORDER BY `id` DESC", "Row");
        $json['userMoney'] = $task_pay['money'];
        if ($task_pay['money'] < $useMoney) {
            $json['useMoney'] = $useMoney;
            $json['status']   = "error";
            $json['msg']      = "支付后请不要修改价格";
            break;
        }
        $updatetask['status'] = 1;
        $task_pay_id          = $task_pay['id'];
        $db->update("cooperation_pay_task", $updatetask, "`id`='$task_pay_id'");
        if ($sqlData['fid']) {
            $fid = $sqlData['fid'];
            $db->delete("task_queue", "`fid`= '$fid'");
            $db->delete("task", "`fid`= '$fid'");
        } else {
            $fid = time() . mt_rand(1000, 9999);
        }
        if ($sqlData['pjdNum']) {
            $redis = new Redis();
            $redis->connect('127.0.0.1', 6379);
            $redis->auth("qqq123123.");
            $dayEndTime = strtotime(date("Y-m-d")) + 86400 - time();
            if (!$redis->exists("scpjd-" . $sqlData['babyId'])) {
                $scpjd['num']   = $sqlData['pjdNum'];
                $scpjd['okNum'] = 0;
                $redis->setex("scpjd-" . $sqlData['babyId'], $dayEndTime, json_encode($scpjd));
            } else {
                $scpjd        = $redis->get("scpjd-" . $sqlData['babyId']);
                $scpjd        = json_decode($scpjd, 1);
                $scpjd['num'] = $sqlData['pjdNum'];
                $redis->setex("scpjd-" . $sqlData['babyId'], $dayEndTime, json_encode($scpjd));
            }
        }
        $uptaskqueue['fid'] = $fid;
        //print_r($sqlData);
        $update['template'] = $data['data'];
        $update['template'] = str_replace("'", "\'", $update['template']);
        if (!$sqlData['time']) {
            $sqlData['time'] = time();
        }
        $taskTime = $sqlData['time'] / 1000;

        $user                   = $_COOKIE['admin'];
        $update['goods2money']  = json_encode($sqlData['goods2money'], JSON_UNESCAPED_UNICODE);
        $update['shop']         = $sqlData['shop'];
        $update['cooperation']  = $admin;
        $update['tknum']        = $sqlData['tknum'];
        $update['fid']          = $fid;
        $update['qnm']          = 1;
        $update['babyId']       = $sqlData['babyId'];
        $update['money']        = $sqlData['money'];
        $update['comMoney']     = 3;
        $update['sort']         = $sqlData['sort'];
        $update['teacherMoney'] = 2;
        $update['tag']          = $sqlData['tag'];
        $update['babyName']     = $sqlData['babyName'];
        preg_match('/^.+?\.(jpg|png|jpeg)/', $sqlData['img'], $img);
        $update['img']             = $img[0];
        $update['addTime']         = $time;
        $update['user']            = $user;
        $update['userGroup']       = $groups[mt_rand(0, 2)];
        $update['userGroup']       = $sqlData['group'];
        $update['lv']              = $sqlData['lv'];
        $update['userGroup']       = $update['userGroup'];
        $uptaskqueue['taskStatus'] = 1;
        $uptaskqueue['link']       = $sqlData['babyId'];
        $uptaskqueue['shop']       = $sqlData['shop'];

        unset($update['user']);
        //unset($update['userGroup']);
        //unset($uptaskqueue['userGroup']);
        $update['person']          = $adminInfo['user'];
        $update['taskStatus']      = '0';
        $uptaskqueue['taskStatus'] = 1;
        $uptaskqueue['userGroup']  = $update['userGroup'];

        foreach ($sqlData['nums'] as $k => $v) {
            if ($sqlData['keywords'][$k]['keyword'] == '') {
                continue;
            }
            unset($update['goods2money']);
            unset($update['sort']);
            if ($sqlData['nums'][$k]['goods2money'] != '无') {
                $update['goods2money'] = '["' . $sqlData['nums'][$k]['goods2money'] . '"]';
            }
            if ($sqlData['nums'][$k]['sort'] != '无') {
                $update['sort'] = $sqlData['nums'][$k]['sort'];
            }
            //$update['bNum'] = json_encode($v['bNum']);
            $update['keywords'] = $sqlData['keywords'][$k]['keyword'];

            $v['yj']['toNum'] = array_filter($v['yj']['toNum']);
            if (count($v['yj']['toNum']) > 0) {
                $update['class'] = 'yj';
                //print_r($update);

                $db->insert('task', $update);
                $uptaskqueue['taskId'] = $db->LAST_INSERT_ID();
                $taskNum               = 0;
                //原价单
                foreach ($v['yj']['toNum'] as $key => $value) {
                    //$uptaskqueue['time']
                    /*   if ($value >= 2) {
                           for ($i = 0; $i < 2; $i++) {
                               $num = $value % 2;
                               $uptaskqueue['taskStartTime'] = $taskTime + $key * 60 * 60 + ($i * 30) * 60;
                               $uptaskqueue['taskEndTime'] = $taskTime + $key * 60 * 60 + ($i * 30) * 60;
                               $i == 1 ? $num = ($value - $value % 2) / 2 + $value % 2 : $num = ($value - $value % 2) / 2;
                               $uptaskqueue['allNum'] = $num;
                               $uptaskqueue['remainingNum'] = $num;
                               $uptaskqueue['addTime'] = time();
                               $db->insert('task_queue', $uptaskqueue);
                           }
                       } else if ($value < 2) {
                           $uptaskqueue['taskStartTime'] = $taskTime + $key * 60 * 60 ;
                           $uptaskqueue['taskEndTime'] = $taskTime + $key * 60 * 60 ;
                           $num = $value;
                           $uptaskqueue['allNum'] = $num;
                           $uptaskqueue['remainingNum'] = $num;
                           $uptaskqueue['addTime'] = time();
                           $db->insert('task_queue', $uptaskqueue);
                       }
                     */
                    $uptaskqueue['taskStartTime'] = $taskTime + $key * 60 * 60;
                    $uptaskqueue['taskEndTime']   = $taskTime + $key * 60 * 60;
                    $uptaskqueue['allNum']        = $value;
                    $uptaskqueue['remainingNum']  = $value;
                    $uptaskqueue['addTime']       = time();
                    $db->insert('task_queue', $uptaskqueue);
                    $taskNum += $value;
                }
                $uptask['toNum']      = $taskNum;
                $uptask['residueNum'] = $taskNum;
                $taskId               = $uptaskqueue['taskId'];

                $db->update("task", $uptask, "`id` = '$taskId'");

            }
            //评价单
            $v['pjd']['toNum'] = array_filter($v['pjd']['toNum']);
            if (count($v['pjd']['toNum']) > 0) {
                $update['class'] = 'pjd';
                $db->insert('task', $update);
                $uptaskqueue['taskId'] = $db->LAST_INSERT_ID();
                $taskNum               = 0;
                foreach ($v['pjd']['toNum'] as $key => $value) {
                    if ($value >= 2) {
                        for ($i = 0; $i < 2; $i++) {
                            $num                          = $value % 2;
                            $uptaskqueue['taskStartTime'] = $taskTime + $key * 60 * 60 + ($i * 30) * 60;
                            $uptaskqueue['taskEndTime']   = $taskTime + $key * 60 * 60 + ($i * 30) * 60;
                            $i == 1 ? $num = ($value - $value % 2) / 2 + $value % 2 : $num = ($value - $value % 2) / 2;
                            $uptaskqueue['allNum']       = $num;
                            $uptaskqueue['remainingNum'] = $num;
                            $uptaskqueue['addTime']      = time();
                            $db->insert('task_queue', $uptaskqueue);
                        }
                    } else if ($value < 2) {
                        $uptaskqueue['taskStartTime'] = $taskTime + $key * 60 * 60;
                        $uptaskqueue['taskEndTime']   = $taskTime + $key * 60 * 60;
                        $num                          = $value;
                        $uptaskqueue['allNum']        = $num;
                        $uptaskqueue['remainingNum']  = $num;
                        $uptaskqueue['addTime']       = time();
                        $db->insert('task_queue', $uptaskqueue);
                    }
                    $taskNum += $value;
                }
                $uptask['toNum']       = $taskNum;
                $uptask['residueNum']  = $taskNum;
                $uptask['goodcomment'] = 1;
                if ($sqlData['pjyh']) {
                    $uptask['coupons']      = $sqlData['key'];
                    $uptask['couponsMoney'] = $sqlData['money2'];
                }

                $taskId = $uptaskqueue['taskId'];
                $db->update("task", $uptask, "`id` = '$taskId'");
                unset($uptask['goodcomment']);
                unset($uptask['coupons']);
                unset($uptask['couponsMoney']);
            }


            //退款单
            $v['tk']['toNum'] = array_filter($v['tk']['toNum']);
            if (count($v['tk']['toNum']) > 0) {
                $update['class'] = 'tk';
                $db->insert('task', $update);
                $uptaskqueue['taskId'] = $db->LAST_INSERT_ID();
                $taskNum               = 0;
                foreach ($v['tk']['toNum'] as $key => $value) {
                    if ($value >= 2) {
                        for ($i = 0; $i < 2; $i++) {
                            $num                          = $value % 2;
                            $uptaskqueue['taskStartTime'] = $taskTime + $key * 60 * 60 + ($i * 30) * 60;
                            $uptaskqueue['taskEndTime']   = $taskTime + $key * 60 * 60 + ($i * 30) * 60;
                            $i == 1 ? $num = ($value - $value % 2) / 2 + $value % 2 : $num = ($value - $value % 2) / 2;
                            $uptaskqueue['allNum']       = $num;
                            $uptaskqueue['remainingNum'] = $num;
                            $uptaskqueue['addTime']      = time();
                            $db->insert('task_queue', $uptaskqueue);
                        }
                    } else if ($value < 2) {
                        $uptaskqueue['taskStartTime'] = $taskTime + $key * 60 * 60;
                        $uptaskqueue['taskEndTime']   = $taskTime + $key * 60 * 60;
                        $num                          = $value;
                        $uptaskqueue['allNum']        = $num;
                        $uptaskqueue['remainingNum']  = $num;
                        $uptaskqueue['addTime']       = time();
                        $db->insert('task_queue', $uptaskqueue);
                    }
                    $taskNum += $value;
                }
                $uptask['toNum']      = $taskNum;
                $uptask['residueNum'] = $taskNum;
                $taskId               = $uptaskqueue['taskId'];
                $db->update("task", $uptask, "`id` = '$taskId'");
            }
            //优惠券单
            $v['yhq']['toNum'] = array_filter($v['yhq']['toNum']);
            if (count($v['yhq']['toNum']) > 0) {
                $update['class'] = 'yhq';
                $db->insert('task', $update);
                $uptaskqueue['taskId'] = $db->LAST_INSERT_ID();
                $taskNum               = 0;
                foreach ($v['yhq']['toNum'] as $key => $value) {
                    if ($value >= 2) {
                        for ($i = 0; $i < 2; $i++) {
                            $num                          = $value % 2;
                            $uptaskqueue['taskStartTime'] = $taskTime + $key * 60 * 60 + ($i * 30) * 60;
                            $uptaskqueue['taskEndTime']   = $taskTime + $key * 60 * 60 + ($i * 30) * 60;
                            $i == 1 ? $num = ($value - $value % 2) / 2 + $value % 2 : $num = ($value - $value % 2) / 2;
                            $uptaskqueue['allNum']       = $num;
                            $uptaskqueue['remainingNum'] = $num;
                            $uptaskqueue['addTime']      = time();
                            $db->insert('task_queue', $uptaskqueue);
                        }
                    } else if ($value < 2) {
                        $uptaskqueue['taskStartTime'] = $taskTime + $key * 60 * 60;
                        $uptaskqueue['taskEndTime']   = $taskTime + $key * 60 * 60;
                        $num                          = $value;
                        $uptaskqueue['allNum']        = $num;
                        $uptaskqueue['remainingNum']  = $num;
                        $uptaskqueue['addTime']       = time();
                        $db->insert('task_queue', $uptaskqueue);
                    }
                    $taskNum += $value;
                }
                $uptask['toNum']      = $taskNum;
                $uptask['residueNum'] = $taskNum;

                $uptask['coupons']      = $sqlData['key'];
                $uptask['couponsMoney'] = $sqlData['money2'];
                $taskId                 = $uptaskqueue['taskId'];
                $db->update("task", $uptask, "`id` = '$taskId'");
                unset($uptask['coupons']);
                unset($uptask['couponsMoney']);
            }

        }
        $json['status'] = "success";
        $json['msg']    = "发布成功";
        break;
    case "addTask":
        $sqlData = json_decode($data['data'], true);
        $user    = $_COOKIE['admin'];
        $type    = $data['type'];

        //$sqlData['keywords'] = json_encode($sqlData['keywords'], JSON_UNESCAPED_UNICODE);

        $update['shop']         = $sqlData['shop'];
        $update['babyId']       = $sqlData['babyId'];
        $update['money']        = $sqlData['money'];
        $update['comMoney']     = $sqlData['comMoney'];
        $update['sort']         = $sqlData['sort'];
        $update['teacherMoney'] = $sqlData['teacherMoney'];
        $update['tag']          = $sqlData['tag'];
        $update['babyName']     = $sqlData['babyName'];
        $update['cooperation']  = $admin;
        if ($sqlData['goodcomment']) {
            $update['goodcomment'] = '1';
        }
        if ($sqlData['goodcomment2']) {
            $update['goodcomment2']   = '1';
            $data['BackupionChangea'] = str_replace(PHP_EOL, '', $data['BackupionChangea']);


            $data['BackupionChangea'] = str_replace("\r\n", '', $data['BackupionChangea']);
            $data['BackupionChangea'] = str_replace("\r", '', $data['BackupionChangea']);
            $data['BackupionChangea'] = str_replace("\n", '', $data['BackupionChangea']);

            $update['goodcomment2json'] = $data['BackupionChangea'];

            $backupionChangea = json_decode($data['BackupionChangea'], 1);
            foreach ($backupionChangea as $k => $v) {
                $backupId               = $v['id'];
                $backupInfo             = $db->query("backup", "`id` = '$backupId'", "Row");
                $babyList               = json_decode($backupInfo['babyList'], 1);
                $babyList[]             = $sqlData['babyId'];
                $upbackup['useNum']     = $backupInfo['useNum'] + 1;
                $upbackup['endUseTime'] = $time;
                $upbackup['babyList']   = json_encode($babyList);
                $db->update("backup", $upbackup, "`id` = '$backupId'");
            }

        }
        $update['goods2money'] = json_encode($sqlData['goods2money'], JSON_UNESCAPED_UNICODE);
        if ($sqlData['couponsCheck']) {
            $update['coupons']      = $sqlData['coupons'];
            $update['couponsMoney'] = $sqlData['couponsMoney'];
        }

        preg_match('/^.+?\.(jpg|png|jpeg)/', $sqlData['img'], $img);
        $update['img']          = $img[0];
        $update['addTime']      = $time;
        $update['user']         = $user;
        $update['userGroup']    = $adminInfo['groups'];
        $update['taskInterval'] = floor(60 / $sqlData['taskIntervalNum']);


        unset($update['user']);
        unset($update['userGroup']);
        $update['person']     = $adminInfo['user'];
        $update['taskStatus'] = '3';

        $i = 0;
        foreach ($sqlData['keywords'] as $k => $v) {
            if ($v != '') {
                if ($i % 2 == 0) {
                    $update['keywords'] = $v;
                } else {
                    $update['toNum']      = $v;
                    $update['residueNum'] = $v;
                    $status               = $db->insert('task', $update);
                }
                $i++;
            }
        }
        // print_r($sqlData);
        /* if ($type == "0") {
             $sqlData['addTime'] = time();
             $sqlData['user'] = $user;
             $sqlData['userGroup'] = $adminInfo['groups'];
             $status = $db->insert('task', $sqlData);
         } else {
             unset($sqlData['id']);
             $status = $db->update('task', $sqlData, "`id` = '$type'");
         }

         $json = sedMsg($status);
         $json['data'] = $sqlData;*/
        $json = sedMsg($status);
        break;

    case "updateTaskStatus":
        $user                 = $data['user'];
        $update['taskStatus'] = $data['status'];

        if ($data['status'] == '0') {
            $arr = $db->query("task", "`id` = '$user'", "Row");
            if ($arr['taskStatus'] == '3') {
                $taskId         = $arr['id'];
                $update['user'] = $_COOKIE['admin'];

                if ($arr['userGroup'] == '') {
                    $update['userGroup'] = $adminInfo['groups'];
                }

                $update['activateTime'] = $time;
                $babyId                 = $arr['babyId'];
                $babyidlist             = $db->query("babyidlist", "`babyId` = '$babyId'");
                if ($babyidlist['id'] && $babyidlist['group'] != $adminInfo['groups']) {
                    $json['status'] = 'error';
                    $json['msg']    = "该链接待" . $babyidlist['group'] . "组激活";
                    break;
                }
                $upBaby['babyId'] = $arr['babyId'];

                $db->insert('babyidlist', $upBaby);
                if ($arr['class'] != '') {
                    $taskId                   = $arr['id'];
                    $updateQueue['userGroup'] = $arr['userGroup'];
                    if ($arr['userGroup'] == '') {
                        $updateQueue['userGroup'] = $adminInfo['groups'];
                    }
                    $updateQueue['taskStatus'] = 1;
                    $db->update('task_queue', $updateQueue, "`taskId` = '$taskId'");
                }
            } else if ($arr['taskStatus'] == '1') {
                $taskId                    = $arr['id'];
                $updateQueue['taskStatus'] = 1;
                $db->update('task_queue', $updateQueue, "`taskId` = '$taskId'");
            }
        }
        if ($data['status'] == 1) {
            $arr = $db->query("task", "`id` = '$user'", "Row");
            if ($arr['fid']) {
                $taskId                    = $arr['id'];
                $updateQueue['taskStatus'] = 3;
                $db->update('task_queue', $updateQueue, "`taskId` = '$taskId'");
            }
        }

        $status             = $db->update('task', $update, "`id` = '$user'");
        $json               = sedMsg($status);
        $json['taskStatus'] = $arr['taskStatus'];
        $json['user']       = $user;
        break;
    case "dedleteTask":
        $user   = $data['user'];
        $status = $db->delete('task_queue', "`taskId` = '$user'");
        $status = $db->delete('task', "`id` = '$user'");

        $json = sedMsg($status);
        break;
}

//账单管理
switch ($id) {
    case "getPhoneBilling":
        $search     = json_decode($data['search'], true);
        $searchName = $search['search'];
        $sql        = " `explain` ='收米吧打款' ";
        if ($search['searchValue'] !== '') {
            $searchValue = $search['searchValue'];
            $sql         .= "AND `$searchName` LIKE '%$searchValue%' ";
        }
        // echo $sql;
        if ($search['filter']['status'] !== '' && isset($search['filter']['status'])) {
            $status = $search['filter']['status'];
            $sql    .= " AND `status` = '$status' ";
        }
        if ($search['filter']['explain'] !== '' && isset($search['filter']['explain'])) {
            $order = $search['filter']['explain'];
            echo $sql .= " AND `explain` LIKE '%$order%' ";
        }
        $total = $db->getCount('billing', '`id`', $sql);
        $pn    = ($search['pn'] - 1) * 20;
        $sql   .= " ORDER BY `id` DESC LIMIT $pn,20";

        $arr = $db->query("billing", $sql);

        foreach ($arr as $k => $v) {
            $arr[$k]['order'] = date('YmdHis', $v['addTime']) * 10000000 + $v['id'] * 1;
        }

        $json['status'] = "success";
        $json['msg']    = "ok";
        $json['total']  = $total;
        $json['data']   = $arr;
        break;
    case "getBillingList":
        $search     = json_decode($data['search'], true);
        $searchName = $search['search'];
        $sql        = ' 1 ';
        if ($search['searchValue'] !== '') {
            $searchValue = $search['searchValue'];
            $sql         .= "AND `$searchName` LIKE '%$searchValue%' ";
        }

        if ($search['filter']['status'] !== '' && isset($search['filter']['status'])) {
            $status = $search['filter']['status'];
            $sql    .= " AND `status` = '$status' ";
        }
        if ($search['filter']['order'] !== '' && isset($search['filter']['order'])) {
            $order = $search['filter']['order'];
            $sql   .= " AND `order` LIKE '%$order%' ";
        }
        if ($search['filter']['explain'] !== '' && isset($search['filter']['explain'])) {
            $order = $search['filter']['explain'];
            $sql   .= " AND `explain` LIKE '%$order%' ";
        }
        $total = $db->getCount('billing', '`id`', $sql);
        $pn    = ($search['pn'] - 1) * 20;
        $sql   .= " ORDER BY `id` DESC LIMIT $pn,20";

        $arr = $db->query("billing", $sql);

        foreach ($arr as $k => $v) {
            $arr[$k]['order'] = date('YmdHis', $v['addTime']) * 10000000 + $v['id'] * 1;
        }
        $json['status'] = "success";
        $json['msg']    = "ok";
        $json['total']  = $total;
        $json['data']   = $arr;
        break;
    case "updateBillingStatus":
        if ($data['status'] == '1') {
            $user     = $data['user'];
            $billings = $db->query("billing", "`id` = '$user'", "Row");
            if ($billings['status'] == '0') {
                $userid = $billings['user'];
                $users  = $db->query("users", "`user` = '$userid'", "Row");


                $openId     = $users['openid'];      //获取openid
                $outTradeNo = $time . uniqid();     //订单号
                $payAmount  = abs($billings['change']);             //转账金额，单位:元。转账最小金额为1元
                $trueName   = '';         //收款人真实姓名
                $result     = $wxPay->createJsBizPackage($openId, $payAmount, $outTradeNo, $trueName, $db);
                if ($result !== true) {
                    $json['status'] = 'error';
                    $json['msg']    = '转账失败，请联系客服';
                    file_put_contents("log\wxpayUser.log", $openid . " " . $payAmount . "\r\n", FILE_APPEND);

                    $update['status'] = 0;

                    break;
                } else {
                    $update['status']  = 1;
                    $update['orderId'] = $outTradeNo;
                    addLog($db, "转账成功", $userid, $update, []);
                }

                $update['status'] = $data['status'];
                $status           = $db->update('billing', $update, "`id` = '$user'");
                if ($status) {
                    $json['status'] = "success";
                    $json['msg']    = "成功!";
                } else {
                    $json['status'] = "error";
                    $json['msg']    = "失败!";
                }
            }
        }
        break;
}
//任务详情
switch ($id) {
    case "getTaskInfoList":
        $search     = json_decode($data['search'], true);
        $searchName = $search['search'];
        $day30time  = time() - 30 * 24 * 60 * 60;
        $sql        = " `addTime` >=  $day30time AND  `person` = '$admin' AND `qnm` = '1' AND (`status` = '1001' OR `status` = '1006')";
        if ($search['searchValue'] !== '') {
            $searchValue = $search['searchValue'];
            $sql         .= "AND `$searchName` = '$searchValue' ";
        }
        if ($search['filter']['status'] !== '' && isset($search['filter']['status'])) {
            $status = $search['filter']['status'];
            $sql    .= " AND `status` = '$status' ";
        }
        if ($search['filter']['payStatus'] !== '' && isset($search['filter']['payStatus'])) {
            $payStatus = $search['filter']['payStatus'];
            $sql       .= " AND `payStatus` = '$payStatus' ";
        }
        if ($search['filter']['buyer_rate'] !== '' && isset($search['filter']['buyer_rate'])) {
            $buyer_rate = $search['filter']['buyer_rate'];
            $sql        .= " AND `buyer_rate` = '$buyer_rate' ";
        }
        if ($search['filter']['refund_status'] !== '' && isset($search['filter']['refund_status'])) {
            $refund_status = $search['filter']['refund_status'];
            switch ($refund_status) {
                case "1":
                    $sql .= " AND `refund_status` = '未退款' ";
                    break;
                case "2":
                    $sql .= " AND `refund_status` = '退款成功' ";
                    break;
                case "4":
                    $sql .= " AND `refund_status` = '买家已经申请退款，等待卖家同意' ";
                    break;
                case "5":
                    $sql .= " AND `refund_status` = '退款关闭' ";
                    break;
                case  "6":
                    $sql .= " AND `refund_status` = '卖家拒绝退款' ";
                    break;
            }
        }

        if ($search['filter']['upStatus'] !== '' && isset($search['filter']['upStatus'])) {
            $upStatus = $search['filter']['upStatus'];

            $upStatus == '1' ? $sql .= " AND `text1` != '' AND `status` ='1001' AND `buyer_rate` !='已评价' " : $sql .= " AND `text1` = '' AND `status` ='1001' AND `buyer_rate` !='已评价' ";
        }

        if ($search['filter']['goodcomment'] !== '' && isset($search['filter']['goodcomment'])) {
            $status = $search['filter']['goodcomment'];
            if ($status) $sql .= " AND `goodcomment` = '1' ";

        }
        $pn = ($search['pn'] - 1) * 20;
        if ($data['complaint'] == 1) {
            $complaintList = $db->query("tklist", "`user` = '$admin'  ORDER BY `id` DESC LIMIT $pn,20");
            $cid           = '';
            foreach ($complaintList as $k => $v) {
                $cid .= "'" . $v['task_id'] . "',";
            }
            $total = $db->getCount('tklist', '`id`', "`user` = '$admin'");
            $sql   .= " `id` in ($cid)";
        } else {
            $total = $db->getCount('tasklist', '`id`', $sql);
            $sql   .= " ORDER BY `id` DESC LIMIT $pn,20";
        }
        $arr = $db->query("tasklist", $sql);

        $cooperation_price     = $db->query("cooperation_price", "`user` = '$admin'", "Row");
        $cooperation_price_arr = json_decode($cooperation_price['info'], 1);

        foreach ($arr as $k => $v) {
            $userOpenid          = $v['openid'];
            $userInfo            = $db->query("users", "`openid` = '$userOpenid'", "Row");
            $teacher             = substr($userInfo['teacher'], 1) - 10000;
            $userInfo            = $db->query("users", "`id` = '$teacher'", "Row");
            $arr[$k]['teacher']  = $userInfo['user'];
            $task_id             = $v['id'];
            $tkInfo              = $db->query("tklist", "`task_id` = '$task_id'", "Row");
            $arr[$k]['tkStatus'] = -1;
            if ($tkInfo['id']) {
                $arr[$k]['tkStatus'] = 1;
                if ($arr['status'] == 0) {
                    $arr[$k]['tkStatus'] = 1;
                } else if ($arr['status2'] == 0 && $arr['status'] == 1) {
                    $arr[$k]['tkStatus'] = 1;
                } else if ($arr['status2'] == 2 || $arr['status'] == 2) {
                    $arr[$k]['tkStatus'] = 2;
                } else if ($arr['status2'] == 1 && $arr['status'] == 1) {
                    $arr[$k]['tkStatus'] = 3;
                }
            }

            $taskId               = $v['taskId'];
            $taskInfo             = $db->query("task", "`id` = '$taskId'", "Row");
            $taskInfo['template'] = preg_replace('/($s*$)|(^s*^)|	/m', '', $taskInfo['template']);
            $taskInfo['template'] = json_decode($taskInfo['template'], true);
            $arr[$k]['template']  = $taskInfo['template'];
            if ($taskInfo['template']['insuranceMoney'] > 0) {
                $arr[$k]['insuranceMoney'] = 1;
            } else {
                $arr[$k]['insuranceMoney'] = 0;
            }

            $v['coupons'] == '' ? $principal = $v['principal'] : $principal = $v['couponsMoney'];;
            $arr[$k]['sjComMoney'] = getSjComMoney($principal, $cooperation_price_arr);
            $arr[$k]['moneyNum']   = $arr[$k]['sjComMoney'];
            $timeday               = strtotime(date("Y-m-d", $v['addTime']));
            $taskId                = $v['id'];
            //$arr[$k]['moneyNum']   = $arr[$k]['sjComMoney'];
            $arr[$k]['moneyNum'] = $db->getNum("tasklist", "principal", "`person` = '$admin' AND `qnm` = '1' AND (`status` = '1001' OR `status` = '1006') AND `coupons` = '' AND `addTime` >= $timeday AND `id` <= '$taskId' ");
            $arr[$k]['moneyNum'] += $db->getNum("tasklist", "couponsMoney", "`person` = '$admin' AND `qnm` = '1' AND (`status` = '1001' OR `status` = '1006')  AND `coupons` !='' AND `addTime` >= $timeday AND `id` <= '$taskId' ");

        }
        $json['status'] = "success";
        $json['total']  = $total;
        $json['msg']    = "ok";
        $json['data']   = $arr;
        break;
    case "updateTaskInfoStatus":
        $user             = $data['user'];
        $update['status'] = $data['status'];

        $status = $db->update('tasklist', $update, "`id` = '$user'");
        $json   = sedMsg($status);
        break;
    case "updateTaskInfoCheck":
        if ($adminInfo['user'] != '刘美君') {
            $json['msg']    = "没有权限";
            $json['status'] = "error";
            break;
        }
        $taskInfoId = $data['user'];
        $arr        = $db->query("tasklist", "`id` = '$taskInfoId'", "Row");

        switch ($arr['status']) {
            case "1"://订单号待审核
                if ($data['status'] == '1001') {
                    $taskStatus['status'] = '1001';
                    $openid               = $arr['openid'];
                    $username             = $arr['user'];
                    $usersInfo            = $db->query("users", "`user` = '$username'", "Row");

                    $users['money']    = $usersInfo['money'] + $arr['principal'];
                    $users['comMoney'] = $usersInfo['comMoney'] + $arr['comMoney'];

                    if ($usersInfo['teacher'] != '') {
                        $teacher = $usersInfo['teacher'];

                        $teacherId   = substr($teacher, 1) - 10000;
                        $teacherInfo = $db->query("users", "`id` = '$teacherId'", "Row");

                        $teachers['comMoney'] = $teacherInfo['comMoney'] + $arr['masterMoney'];
                        $db->update('users', $teachers, "`id` = '$teacherId'");

                    } else {
                        $users['comMoney'] = $users['comMoney'] + $arr['masterMoney'];
                    }
                    $wechat = new wechat($db);
                    addLog($db, "订单号审核通过", $usersInfo['user'], $users, []);
                    $wechat->sedTemplate($openid, $webUrl, "订单号审核成功", $arr['id'] + 100000, "订单号审核成功");

                } else if ($data['status'] == '2001') {
                    $taskStatus['status'] = '2001';
                    $wechat               = new wechat($db);
                    $wechat->sedTemplate($openid, $webUrl, "订单号审核失败", $arr['id'] + 100000, "订单号审核失败");

                }
                $users['taskStatus'] = '0';
                $db->update('users', $users, "`user` = '$username'");
                break;
            case "2"://收货待审核
                if ($data['status'] == '3') {
                    $taskStatus['status'] = '3';
                    $openid               = $arr['openid'];
                    $wechat               = new wechat($db);
                    $wechat->sedTemplate($openid, $webUrl, "订单已完成", $arr['id'] + 100000, "订单已完成");

                } else if ($data['status'] == '2002') {
                    $taskStatus['status'] = '2002';
                    $openid               = $arr['openid'];
                    $usersInfo            = $db->query("users", "`openid` = '$openid'", "Row");
                    $users['money']       = $usersInfo['money'] - $arr['principal'];
                    $users['comMoney']    = $usersInfo['comMoney'] - $arr['comMoney'];
                    $db->update('users', $users, "`openid` = '$openid'");
                    $wechat = new wechat($db);
                    $wechat->sedTemplate($openid, $webUrl, "收货审核失败", $arr['id'] + 100000, "收货审核失败");

                }
                break;
            case "1001"://订单号审核通过

                $username = $arr['user'];
                if ($data['status'] == '2') {
                    $taskStatus['status'] = '2';

                } else if ($data['status'] == '1' || $data['status'] == '2001') {
                    $taskStatus['status'] = $data['status'];

                    $usersInfo = $db->query("users", "`user` = '$username'", "Row");
                    if ($usersInfo['role'] == '1') {
                        $teacher     = $usersInfo['teacher'];
                        $teacherId   = substr($sqlData['teacher'], 1) - 10000;
                        $teacherInfo = $db->query("users", "`id` = '$teacherId'", "Row");

                        $teachers['comMoney'] = $teacherInfo['comMoney'] - $arr['masterMoney'];
                        $db->update('users', $teachers, "`id` = '$teacherId'");

                    }
                    $users['money']    = $usersInfo['money'] - $arr['principal'];
                    $users['comMoney'] = $usersInfo['comMoney'] - $arr['comMoney'];

                    $db->update('users', $users, "`user` = '$username'");
                }

                break;
            case "2001"://订单号审核失败
                if ($data['status'] == '1001') {
                    $taskStatus['status'] = '1001';
                    $username             = $arr['user'];
                    $openid               = $arr['openid'];
                    $usersInfo            = $db->query("users", "`user` = '$username'", "Row");
                    if ($usersInfo['role'] == '1') {
                        $teacher     = $usersInfo['teacher'];
                        $teacherId   = substr($sqlData['teacher'], 1) - 10000;
                        $teacherInfo = $db->query("users", "`id` = '$teacherId'", "Row");

                        $teachers['comMoney'] = $teacherInfo['comMoney'] + $arr['masterMoney'];
                        $db->update('users', $teachers, "`id` = '$teacherId'");

                    }
                    $users['money']    = $usersInfo['money'] + $arr['principal'];
                    $users['comMoney'] = $usersInfo['comMoney'] + $arr['comMoney'];
                    $db->update('users', $users, "`openid` = '$openid'");
                    $wechat = new wechat($db);
                    $wechat->sedTemplate($openid, $webUrl, "订单号审核成功", $arr['id'] + 100000, "订单号审核成功");
                }
                break;
            case "2002"://收货审核失败
                if ($data['status'] == '3') {
                    $taskStatus['status'] = '3';
                    $openid               = $arr['openid'];
                    $usersInfo            = $db->query("users", "`openid` = '$openid'", "Row");

                    if ($usersInfo['role'] == '1') {
                        $teacher     = $usersInfo['teacher'];
                        $teacherId   = substr($sqlData['teacher'], 1) - 10000;
                        $teacherInfo = $db->query("users", "`id` = '$teacherId'", "Row");

                        $teachers['comMoney'] = $teacherInfo['comMoney'] + $arr['masterMoney'];
                        $db->update('users', $teachers, "`id` = '$teacherId'");

                    }
                    $users['money']    = $usersInfo['money'] + $arr['principal'];
                    $users['comMoney'] = $usersInfo['money'] + $arr['comMoney'];

                    $db->update('users', $users, "`openid` = '$openid'");
                    $wechat = new wechat($db);
                    $wechat->sedTemplate($openid, $webUrl, "订单号完成", $arr['id'] + 100000, "订单完成");

                }
                break;
            case "3"://完成
                if ($data['status'] == '2002') {
                    $taskStatus['status'] = '2002';
                    $openid               = $arr['openid'];
                    $usersInfo            = $db->query("users", "`openid` = '$openid'", "Row");
                    if ($usersInfo['role'] == '1') {
                        $teacher     = $usersInfo['teacher'];
                        $teacherId   = substr($sqlData['teacher'], 1) - 10000;
                        $teacherInfo = $db->query("users", "`id` = '$teacherId'", "Row");

                        $teachers['comMoney'] = $teacherInfo['comMoney'] - $arr['masterMoney'];
                        $db->update('users', $teachers, "`id` = '$teacherId'");

                    }
                    $users['money']    = $usersInfo['money'] - $arr['principal'];
                    $users['comMoney'] = $usersInfo['comMoney'] - $arr['comMoney'];
                    $db->update('users', $users, "`openid` = '$openid'");
                }
                break;
        }
        if ($taskStatus['status']) {
            $status = $db->update('tasklist', $taskStatus, "`id` = '$taskInfoId'");
        }
        $json['msg']    = "ok";
        $json['status'] = "success";
        break;
}
//设置页面
switch ($id) {
    case "getSetting":
        switch ($data['name']) {
            case "wechatform":
                $sql = "`name` = 'wechat_config'";
                break;
            case "money":
                $sql = "`name` = 'money'";
                break;
        }
        $arr          = $db->query("setting", $sql, "Row");
        $json['msg']  = "ok";
        $json['data'] = json_decode($arr['value'], true);
        break;
    case "addMoneyUp":
        $update['value'] = $data['money'];
        $db->update('setting', $update, "`name` = 'money'");
        break;
}
//公共方法

switch ($id) {
    /*    case "setArr":
            $table = $data['table'];
            $arr = json_decode($data['arr'], true);
            $sql = ' `id` in (';
            foreach ($arr as $k => $v) {
                $tableId = $v['id'];
                $sql .= " '$tableId',";
            }
            $sql = rtrim($sql, ",");

            switch ($data['set']) {
                case 'delete';
                    $status = $db->delete($table, $sql . ")");
                    break;
            }

            $json = sedMsg($status);
            break;*/
}
$endtime          = explode(' ', microtime());
$thistime         = $endtime[0] + $endtime[1] - ($starttime[0] + $starttime[1]);
$thistime         = round($thistime, 3);
$json['thistime'] = $thistime;
echo json_encode($json);


function sedMsg($status)
{
    if ($status) {
        $json['status'] = "success";
        $json['msg']    = "成功!";
    } else {
        $json['status'] = "error";
        $json['msg']    = "失败!";
    }

    return $json;
}

function getStatus($code)
{
    switch ($code) {
        case "NO_REFUND":
            $refundStatus = "未退款";
            break;
        case "WAIT_SELLER_AGREE":
            $refundStatus = "买家已经申请退款，等待卖家同意";
            break;
        case "WAIT_BUYER_RETURN_GOODS":
            $refundStatus = "卖家已经同意退款，等待买家退货";
            break;
        case "WAIT_SELLER_CONFIRM_GOODS":
            $refundStatus = "买家已经退货，等待卖家确认收货";
            break;
        case "SELLER_REFUSE_BUYER":
            $refundStatus = "卖家拒绝退款";
            break;
        case "CLOSED":
            $refundStatus = "退款关闭";
            break;
        case "SUCCESS":
            $refundStatus = "退款成功";
            break;
        default:
            $refundStatus = "获取错误";
    }
    return $refundStatus;
}

function curlGet($url = '', $options = [])
{
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    if (!empty($options)) {
        curl_setopt_array($ch, $options);
    }
    //https请求 不验证证书和host
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}

function getSjCommoney($money, $cooperation_price_arr)
{
    switch (true) {
        case $money <= 100:
            $cooperation_price = $cooperation_price_arr[0];
            break;
        case $money <= 150:
            $cooperation_price = $cooperation_price_arr[1];
            break;
        case $money <= 250:
            $cooperation_price = $cooperation_price_arr[2];
            break;
        case $money <= 350:
            $cooperation_price = $cooperation_price_arr[3];
            break;
        case $money <= 450:
            $cooperation_price = $cooperation_price_arr[4];
            break;
        case $money <= 500:
            $cooperation_price = $cooperation_price_arr[5];
            break;
        case $money <= 800:
            $cooperation_price = $cooperation_price_arr[6];
            break;
        case $money <= 1000:
            $cooperation_price = $cooperation_price_arr[7];
            break;
    }
    return $cooperation_price;
}

function tk($data, $db, $taskListInfo, $adminInfo, $admin)
{

    $taskListId = $data['taskId'];
    $babyId     = $taskListInfo['babyId'];
    $day10Time  = time() - 10 * 24 * 60 * 60;
    $day10Num   = $db->getCount("tasklist", "1", "`babyId` = '$babyId' AND status in (1001,1006) AND `addTime` >= $day10Time");
//    if ($taskListInfo['addTime'] >= time() - 24 * 60 * 60) {
//        $json['msg'] = '只能提交一天后的订单';
//        $json['status'] = 'error';
//        break;
//    }
    if ($taskListInfo['addTime'] <= time() - 10 * 24 * 60 * 60) {
        $json['msg']    = '只能提交10天内订单';
        $json['status'] = 'error';
        return $json;
    }

    $shop  = $taskListInfo['shop'];
    $order = $taskListInfo['order'];

    $url           = "http://114.55.144.79/taobao/TradeFullinfoGetRequest.php?shop=$shop&tid=$order&xs=1";
    $xsJson        = curlGet($url);
    $xsJson        = json_decode($xsJson, 1);
    $refund_status = $xsJson['trade']['orders']['order'][0]['refund_status'];
    $refund_id     = @$xsJson['trade']['orders']['order'][0]['refund_id'];
    $url           = "http://114.55.144.79/taobao/RefundGetRequest.php?shop=$shop&tid=$refund_id";
    $refundInfo    = curlGet($url);
    $orderInfo     = json_decode($refundInfo, 1);
    $modified      = @$orderInfo['refund']['modified'];


    $update['refund_status'] = getStatus($refund_status);

    if (strtotime($modified) + 24 * 60 * 60 < time() && $update['refund_status'] == '退款成功') {
        $json['msg']    = '未在退款后24小时内提交';
        $json['status'] = 'error';
        return $json;
    }


    $update['money'] = $taskListInfo['principal'];
    if ($taskListInfo['coupons'] != '') {
        $update['money'] = $taskListInfo['couponsMoney'];
    }

    $taskId       = $taskListInfo['taskId'];
    $taskInfo     = $db->query("task", "`id` = '$taskId'", "Row");
    $templateInfo = $taskInfo['template'];
    $templateInfo = json_decode(preg_replace('/($s*$)|(^s*^)|	/m', '', $templateInfo), 1);
    if ($templateInfo['insuranceMoney'] > 0) { //买了保险
        $update['insurance']      = 1;
        $update['insuranceMoney'] = $templateInfo['insuranceMoney'];


    } else {  //未买保险
        if ($day10Num < 1000) { //日平均单量少于100
            $json['msg']    = '该链接十天内日平均单量少于100，不能提交';
            $json['status'] = 'error';
            return $json;
        }

    }

    $update['task_id'] = $taskListId;
    $update['user']    = $admin;
    $update['addTime'] = date("Y-m-d H:i:s");
    $db->insert("tklist", $update);

    $update              = [];
    $update['complaint'] = 1;
    $db->update("tasklist", $update, "`id` = '$taskListId'");


    $json['msg']    = '提交成功，等待审核';
    $json['status'] = 'success';
    return $json;
}


function getComMoney($money)
{
    switch (true) {
        case $money < 300;
            return 3;
        case $money < 500;
            return 4;
        case $money < 600;
            return 5;
        case $money < 700;
            return 6;
        case $money < 800;
            return 7;
        case $money < 900;
            return 8;
        case $money < 1000;
            return 9;
    }

}
